Creates a `!house` command that determines which house from Harry Potter you belong to. Can be customised easily to sort users for your server clans for example. Sorting is random but based on the user ID so it cannot be changed without changing the hash seed in code..
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const { channel_id, content, author: {id: user_id} } = context.params.event;
const command = `!house`;
const hashSeed = 'harry-potter'; // Update to your starting seed
const clans = [
{
name: 'Gryffindor',
embed: {
title: '🦁 Gryffindor',
color: 0xff2400, // scarlet
fields: [
{name: 'Values', value: 'Bravery, daring, nerve, and chivalry'},
{
name: 'Hat says',
value:
'You might belong in Gryffindor, Where dwell the brave at heart, Their daring, nerve, and chivalry Set Gryffindors apart.',
},
],
},
},
{
name: 'Hufflepuff',
embed: {
title: '🦡 Hufflepuff',
color: 0xffff00, // yellow
fields: [
{
name: 'Values',
value: 'Hard work, dedication, patience, loyalty, and fair play',
},
{
name: 'Hat says',
value:
'You might belong in Hufflepuff, Where they are just and loyal, Those patient Hufflepuffs are true, And unafraid of toil.',
},
],
},
},
{
name: 'Ravenclaw',
embed: {
title: '🦅 Ravenclaw',
color: 0x0000ff, // blue
fields: [
{
name: 'Values',
value: 'Intelligence, knowledge, curiosity, creativity and wit',
},
{
name: 'Hat says',
value:
"You might belong in wise old Ravenclaw, If you've a ready mind, Where those of wit and learning, Will always find their kind.",
},
],
},
},
{
name: 'Ravenclaw',
embed: {
title: '🐍 Slytherin',
color: 0x00ff00, // green
fields: [
{
name: 'Values',
value:
'Ambition, leadership, self-preservation, cunning and resourcefulness',
},
{
name: 'Hat says',
value:
"You might belong in Slytherin, You'll make your real friends, Those cunning folk use any means, To achieve their ends.",
},
],
},
},
];
if (content.trim() !== command) return;
// Turn string into a number
// https://stackoverflow.com/a/65239086/3801481
const hashString = (str) => {
let hash = 0,
_str = `${hashSeed}-${str}`;
for (let i = 0; i < _str.length; ++i)
hash = Math.imul(31, hash) + _str.charCodeAt(i);
return Math.abs(hash | 0);
};
// Randomly choose clan based on the user ID
const clanIndex = hashString(user_id) % clans.length;
const clan = clans[clanIndex];
await lib.discord.channels['@0.1.2'].messages.create({
channel_id,
content: `Welcome to ${clan.name} <@!${user_id}>`,
embed: clan.embed,
});