-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.js
141 lines (133 loc) · 4.8 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Author: Aidan Makare
* Brief: A fun, discord bot project that implements a rhyming game to play amongst friends in a discord server.
*/
var Discord = require("discord.io");
const axios = require("axios");
const fetch = require("node-fetch");
var randomWords = require("random-words");
require("dotenv").config();
const {
fillJsonArray,
initialMessage,
followUpMessage,
finalMessage,
} = require("./functions");
//Necassary variables for game - will eventually be a Game class as seen in test branch for when this bot is (if ever) publicized.
var rData = [];
var word = [];
var check;
var counter;
var flag = true;
var maxCounter = 10;
//Creating bot with token
var bot = new Discord.Client({
token: process.env.BOTTOKEN,
autorun: true,
});
bot.setMaxListeners(0);
//Logging to console when bot is online
bot.on("ready", () => {
console.log("EzRhyme Bot is online");
});
//Functionality
bot.on("message", (user, userID, channelID, message, evt) => {
//Command prefix created
if (message.substring(0, 1) == "^") {
var args = message.substring(1).split(" ");
var cmd = args[0];
args = args.splice(1);
//Simple about the bot command
if (cmd === "about") {
bot.sendMessage({
to: channelID,
message:
"A fun, rhyming game bot! Work amongst your Discord friends to come up words that rhyme with a randomly generated word! \n**Author:** @Ezrue#4297 on Discord \n**Github:** github.com/Ajmakare/DiscordRhymeBot",
});
return;
}
//Reset current game if word is too difficult, or if for some reason the bot is to break.
if (cmd === "reset") {
rData = [];
flag = true;
check = undefined;
maxCounter = 10;
bot.sendMessage({
to: channelID,
message:
":grimacing: **" +
user +
"** reset the game. Do ^rhyme to start a new one! :hugging:",
});
return;
}
/*Main bot functionality/concept:
*see README*
*/
//Generates a random word and uses that word to retrieve the rhyming JSON from the datamuse API
//It then fills an array, rData, with all the words in the JSON that rhyme with the random word generated
if (cmd === "rhyme") {
if (flag) {
word = randomWords(1);
flag = false;
}
fetch(`https://api.datamuse.com/words?rel_rhy=${word}`)
.then((res) => res.json())
.then((json) => {
let rhymeJson = JSON.stringify(json);
if (rhymeJson.charAt(1) != "]") {
rhymeJson = JSON.parse(rhymeJson);
check = fillJsonArray(rhymeJson, rData); //Helper function defined in functions.js
counter = rData.length;
if (counter >= 10) {
initialMessage(channelID, word, counter, check, maxCounter, bot); //Helper function defined in functions.js
//Bot listens to messages in channel and announces if a word has been said that rhymes with the generated word
bot.on(
"message",
(user, userID, channelID, message2, rawEvent) => {
message2 = message2.toLowerCase();
if (maxCounter != 0) {
for (let j = 1; j < rData.length; j++) {
//console.log(rData[j]);
if (message2 != "^rhyme") {
if (message2 === rData[j]) {
rData = rData.filter((e) => e !== message2);
counter--;
maxCounter--;
if (maxCounter == 0) {
finalMessage(channelID, message2, word, bot); //Helper function defined in functions.js
rData = [];
flag = true;
check = undefined;
maxCounter = 10;
return;
}
followUpMessage(
channelID,
message2,
word,
maxCounter,
counter,
bot
); //Helper function defined in functions.js
}
}
}
}
}
);
} else {
console.error("Counter error"); //Throws error if counter is for some reason off (more for my bug checking)
rData = [];
flag = true;
check = undefined;
maxCounter = 10;
}
} else {
console.error("Invalid word error"); //Throws this error if random word module generates a word that the API doesn't contain.
flag = true;
}
});
}
}
});