Skip to content

Commit

Permalink
restructure and handle poor sources
Browse files Browse the repository at this point in the history
  • Loading branch information
thezanke committed Jan 27, 2021
1 parent 6febe2c commit e33b4b6
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 151 deletions.
83 changes: 83 additions & 0 deletions src/handle_commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { Message } from "https://deno.land/x/discordeno@10.1.0/mod.ts";
import { getScore, REACTION_SCORES } from "./scoring.ts";

const COMMAND_TRIGGER = "ph8";

const commandHandlers: {
[command: string]: (
message: Message,
...args: Array<string | undefined>
) => void;
} = {
help(message, topic) {
if (!topic) {
message.reply(
[
"Topics: `scoring`.",
"",
`Say "${COMMAND_TRIGGER} help [topic]".`,
].join("\n")
);
return;
}

switch (topic) {
case "scoring":
message.reply(
[
"You can react to messages with certain emoji to add or subtract from a users score.",
"",
"Reactions and their values: ",
...Object.keys(REACTION_SCORES).map(
(emoji) => `${emoji} == ${REACTION_SCORES[emoji]}`
),
"",
`You can retrieve your score by saying "${COMMAND_TRIGGER} my score".`,
`You can retrieve other users\' scores by saying "${COMMAND_TRIGGER} score @username".`,
].join("\n")
);
break;
}
},
my(message, subCommand) {
if (subCommand === "score") {
const score = getScore(message.author.id);
message.reply(`${score || 0}`);
}
},

score(message) {
if (message.mentions.length) {
message.reply(
message.mentions
.map((userID) => {
const score = getScore(userID);
return `<@${userID}> has ${score} points`;
})
.join("\n")
);
}
},
};

export const handleCommands = (message: Message) => {
if (!message.content.toLowerCase().startsWith(COMMAND_TRIGGER)) return;

const [, command, ...args] = message.content.split(" ");

console.log({ command, args });

if (!command) {
message.reply("Yeah, bud?");
return true;
}

const handler = commandHandlers[command];
if (handler) {
handler(message, ...args);
return true;
}

message.reply("I'm not sure what that means, m8.");
return true;
};
11 changes: 7 additions & 4 deletions src/handle_poor_sources.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { Message } from "https://deno.land/x/discordeno@10.1.0/mod.ts";
import config from "./config.ts";
import { pickRandom } from "./utils.ts";

const REPLIES = ["uhhhh....", "yikeees.", "hahhahhahahahaha", "oh boy", "🍿🤡"];

export const handlePoorSources = (message: Message) => {
if (!config.POOR_SOURCES) return;
if (!config.POOR_SOURCES?.length) return;

const poorSources = config.POOR_SOURCES.join("|");
const re = new RegExp(`https?:\/\/(?: ${poorSources})`, "gi");
const re = new RegExp(`https?://(?:[a-z]+.)+(?:${poorSources})`, "gi");

if (!re.test(message.content)) return;

message.reply('uhh.... :grimace:');
message.reply(pickRandom(REPLIES));

return true;
};
38 changes: 38 additions & 0 deletions src/handle_reactions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
MessageReactionUncachedPayload,
ReactionPayload,
Message,
} from "https://deno.land/x/discordeno@10.1.0/mod.ts";
import { getScore, REACTION_SCORES, setScore } from "./scoring.ts";

interface ReactionHandler {
(
payload: MessageReactionUncachedPayload,
emoji: ReactionPayload,
userID: string,
message?: Message,
removal?: Boolean
): any;
}

export const handleReactions: ReactionHandler = (
_p,
emoji,
userID,
message,
remove = false
) => {
if (!message) return;
if (!emoji.name?.length) return;

let value = REACTION_SCORES[emoji.name];
if (!value) return;

if (remove) value *= -1;

const messageAuthorID = message.author.id;
if (!messageAuthorID || messageAuthorID === userID) return;

let lastScore = getScore(messageAuthorID);
setScore(messageAuthorID, lastScore + value);
};
153 changes: 6 additions & 147 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,158 +1,17 @@
import {
startBot,
MessageReactionUncachedPayload,
ReactionPayload,
Message,
botID,
} from "https://deno.land/x/discordeno@10.1.0/mod.ts";
import config from "./config.ts";
import { handleCommands } from "./handle_commands.ts";
import { handleDigression } from "./handle_digression.ts";
import { handlePoorSources } from "./handle_poor_sources.ts";
import { handleReactions } from "./handle_reactions.ts";
import { server } from "./http_server.ts";
import { getScore, setScore } from "./scoring.ts";

interface ReactionHandler {
(
payload: MessageReactionUncachedPayload,
emoji: ReactionPayload,
userID: string,
message?: Message,
removal?: Boolean
): any;
}

const REACTION_SCORES: { [reaction: string]: number | undefined } = {
"1️⃣": 1,
"2️⃣": 2,
"3️⃣": 3,
"4️⃣": 4,
"5️⃣": 5,
"6️⃣": 6,
"7️⃣": 7,
"8️⃣": 8,
"9️⃣": 9,
"🔟": 10,
"0️⃣": -10,
"💯": 10,
"🏆": 10,
"👍": 1,
"👎": -1,
"⬆️": 1,
"⬇️": -1,
"👏": 1,
"😂": 1,
"🤣": 1,
"🍆": -3,
"💩": -5,
"🤡": -10,
};

const getReactionValue = (emojiName?: string | null) =>
emojiName && emojiName.length && REACTION_SCORES[emojiName];

const handleScoreReactions: ReactionHandler = (
_p,
emoji,
userID,
message,
remove = false
) => {
if (!message) return;

let value = getReactionValue(emoji.name);
if (!value) return;
if (remove) value *= -1;

const messageAuthorID = message.author.id;
if (!messageAuthorID || messageAuthorID === userID) return;

let lastScore = getScore(messageAuthorID);
setScore(messageAuthorID, lastScore + value);
};

const BOT_TRIGGER = "ph8";

const commandHandlers: {
[command: string]: (
message: Message,
...args: Array<string | undefined>
) => void;
} = {
help(message, topic) {
if (!topic) {
message.reply(
["Topics: `scoring`.", "", `Say "${BOT_TRIGGER} help [topic]".`].join(
"\n"
)
);
return;
}

switch (topic) {
case "scoring":
message.reply(
[
"You can react to messages with certain emoji to add or subtract from a users score.",
"",
"Reactions and their values: ",
...Object.keys(REACTION_SCORES).map(
(emoji) => `${emoji} == ${REACTION_SCORES[emoji]}`
),
"",
`You can retrieve your score by saying "${BOT_TRIGGER} my score".`,
`You can retrieve other users\' scores by saying "${BOT_TRIGGER} score @username".`,
].join("\n")
);
break;
}
},
my(message, subCommand) {
if (subCommand === "score") {
const score = getScore(message.author.id);
message.reply(`${score || 0}`);
}
},

score(message) {
if (message.mentions.length) {
message.reply(
message.mentions
.map((userID) => {
const score = getScore(userID);
return `<@${userID}> has ${score} points`;
})
.join("\n")
);
}
},
};

const handleFunnyReplies = (message: Message) => {
handleDigression(message) || handlePoorSources(message);
};

const handleCommandMessages = (message: Message) => {
if (!message.content.toLowerCase().startsWith(BOT_TRIGGER)) {
handleFunnyReplies(message);
return;
}

const [, command, ...args] = message.content.split(" ");

console.log({ command, args });

if (!command) {
message.reply("Yeah, bud?");
return;
}

const handler = commandHandlers[command];
if (handler) {
handler(message, ...args);
return;
}

message.reply("I'm not sure what that means, m8.");
return handleDigression(message) || handlePoorSources(message) || console.log(message);
};

startBot({
Expand All @@ -164,17 +23,17 @@ startBot({
console.log(`-> botID: ${botID}`);
},
messageCreate(message) {
handleCommandMessages(message);
handleCommands(message) || handleFunnyReplies(message);
},
reactionAdd(...args) {
const [, emoji] = args;
if (!emoji.name) return;
handleScoreReactions(...args);
handleReactions(...args);
},
reactionRemove(...args) {
const [, emoji] = args;
if (!emoji.name) return;
handleScoreReactions(...args, true);
handleReactions(...args, true);
},
},
});
Expand Down
27 changes: 27 additions & 0 deletions src/scoring.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
const SCORES_FILE_PATH = "./data/scores.json";


export const REACTION_SCORES: { [reaction: string]: number | undefined } = {
"1️⃣": 1,
"2️⃣": 2,
"3️⃣": 3,
"4️⃣": 4,
"5️⃣": 5,
"6️⃣": 6,
"7️⃣": 7,
"8️⃣": 8,
"9️⃣": 9,
"🔟": 10,
"0️⃣": -10,
"💯": 10,
"🏆": 10,
"👍": 1,
"👎": -1,
"⬆️": 1,
"⬇️": -1,
"👏": 1,
"😂": 1,
"🤣": 1,
"🍆": -3,
"💩": -5,
"🤡": -10,
};

const scores: { [userID: string]: number } = {};

// hydrate scores from file
Expand Down
1 change: 1 addition & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const pickRandom = (arr: any[]) => arr[Math.floor(Math.random() * arr.length)];

0 comments on commit e33b4b6

Please sign in to comment.