forked from siddhu33/discordle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
107 lines (98 loc) · 2.82 KB
/
bot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Run dotenv
require('dotenv').config();
const fs = require('fs');
const readline = require('readline');
const { Client, Intents } = require('discord.js');
const { defaultState, commands } = require('./commands');
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
],
});
const CORPUS_PATH = 'corpus.txt';
const stream = fs.createReadStream(CORPUS_PATH);
const myInterface = readline.createInterface({
input: stream,
});
const gamesByChannel = {
};
const wordsByLength = {};
const loadCorpus = async () => {
console.log('starting...');
for await (const line of myInterface) {
const lengthStr = line.length.toString();
if (Object.keys(wordsByLength).includes(lengthStr)) {
wordsByLength[lengthStr].push(line);
} else {
wordsByLength[lengthStr] = [line];
}
}
console.log('corpus word statistics:');
console.table(Object.entries(wordsByLength).map((e) => ({ length: e[0], count: e[1].length })));
};
loadCorpus();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
const parse = (msg) => {
const tokens = msg.content.split(' ');
for (let i = 0; i < tokens.length; i += 1) {
const token = tokens[i];
if (token.startsWith('!')) {
const command = token.substring(1);
if (command === 'ping' || command === 'state' || command === 'end') {
return {
command,
args: [],
errorMsg: '',
};
}
if (i + 1 < tokens.length) {
return {
command,
args: tokens.slice(i + 1),
errorMsg: '',
};
}
return {
command: 'error',
args: [],
errorMsg: `Could not find arguments for command ${command}.`,
};
}
}
return {
command: 'error',
args: [],
errorMsg: 'Could not find a valid command in message',
};
};
const executeCommand = (msg, commandObj) => {
const commandFunc = commands[commandObj.command];
if (commandFunc != null) {
const gameState = gamesByChannel[msg.channelId] || defaultState();
const replyObj = commandFunc(commandObj.args, wordsByLength, gameState);
gamesByChannel[msg.channelId] = replyObj.gameState;
return replyObj.reply;
}
return `Command ${commandObj.command} is not valid.`;
};
client.on('messageCreate', (msg) => {
const botMentioned = Boolean(msg.mentions.users.get(client.user.id));
const messageNotBot = msg.author.id !== client.user.id;
if (botMentioned && messageNotBot) {
const commandObj = parse(msg);
try {
if (commandObj.command === 'error') {
throw commandObj.errorMsg;
} else {
msg.reply(executeCommand(msg, commandObj));
}
} catch (error) {
console.error(error);
msg.reply(`error: ${JSON.stringify(error)}`);
}
}
});
client.login(process.env.DISCORD_TOKEN);