-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
288 lines (270 loc) · 10.3 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
const Discord = require('discord.js');
const config = require('./config.json')
const fs = require("fs")
var logger = require('winston');
var request = require('request');
const bot = new Discord.Client({
token: config.token,
autorun: true,
name: "MarvinBot"
});
// Configure logger settings
logger.remove(logger.transports.Console);
logger.add(logger.transports.Console, {
colorize: true
});
logger.level = 'silly';
// Initialize Discord Bot
bot.on('ready', function (evt) {
logger.info('Connected');
logger.info('Logged in as: ');
logger.info(bot.username + ' - (' + bot.id + ')');
});
// set bot's game
bot.on('ready', () => {
bot.user.setGame(config.game);
});
// listens for messages
bot.on('message', (message) => {
// if message doesn't start with the prefix or is sent from the bot itself
// the bot will not do anything
if (!message.content.startsWith(config.prefix) || message.author.bot) return;
// separates the command and arguments
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
// !prefix
// change the prefix
if(command === "prefix") {
let newPrefix = args[0];
// change the configuration in memory
config.prefix = newPrefix;
// Now we have to save the file.
fs.writeFile("./config.json", JSON.stringify(config), (err) => console.error);
message.reply("Prefix changed to **" + config.prefix + "**").then(function (message) {
message.react(config.delemoji);
logger.info("got command !prefix");
})
}
// sets the bot's game (activity)
if(command === "game") {
if(args[0]){
config.game = args.join(" ");
fs.writeFile("./config.json", JSON.stringify(config), (err) => console.error);
bot.user.setGame(config.game);
message.channel.send("Set bot 'game' to " + config.game).then(function (message) {
message.react(config.delemoji);
logger.info("got command !game")
})
}
else{
message.channel.send("Give me a game to play!").then(function (message) {
message.react(config.delemoji);
logger.info("got command !game")
})
}
}
// change the emoji which deletes bot messages
/*if(command === "delemoji") {
// newdelemoji is the EMOJI, found through its NAME
const newdelemoji = bot.emojis.find("name", args[0]);
// config.delemojistuff is the NAME and ID of the EMOJI
config.delemojistuff = newdelemoji.toString();
config.delemojiid = newdelemoji.id
// Now we have to save the file.
fs.writeFile("./config.json", JSON.stringify(config), (err) => console.error);
message.reply("New delemoji reaction changed to **" + config.delemojistuff + "**").then(function (message) {
message.react(config.delemojiid);
})
message.reply("New delemoji reaction changed to **" + config.delemojiid + "**").then(function (message) {
message.react(config.delemojiid);
})
}*/
// repeats what the user said
if(command === "say") {
if(args[0]){
// makes the bot say something and delete the message. As an example, it's open to anyone to use.
// To get the "message" itself we join the `args` back into a string with spaces:
const sayMessage = args.join(" ");
// Then we delete the command message (sneaky, right?). The catch just ignores the error with a cute smiley thing.
message.delete().catch(O_o=>{});
// And we get the bot to say the thing:
message.channel.send(sayMessage);
logger.info("got command !say");
}else{
message.channel.send("I've got nothing to say. 🤐").then(function (message) {
message.react(config.delemoji);
logger.info("got command !say");
});
}
}
// change the time to wait before deleting bot message due to reaction
if(command === "deltime"){
if (!args[0]){
message.channel.send("Enter time to wait before deleting bot message in ms. Example:\n!deltime 1000").then(function (message) {
message.react(config.delemoji);
logger.info("got command !deltime");
});
}else{
let newdeltime = args[0];
if (newdeltime < 0 || newdeltime > 10000){
message.channel.send("Time to wait before deleting bot message should be > 0 and < 10000 (in ms)").then(function (message) {
message.react(config.delemoji);
logger.info("got command !deltime");
});
}else{
config.deltime = newdeltime;
fs.writeFile("./config.json", JSON.stringify(config), (err) => console.error);
message.channel.send("Time to wait before deleting bot message set to " + config.deltime + " ms.").then(function (message) {
message.react(config.delemoji);
logger.info("got command !deltime");
});
}
}
}
// checks if twitch user is streaming
if(command === "online"){
if(!args[0]){
message.channel.send("Who?");
}else{
var answer;
let channel = args[0];
request({
headers: {
'Client-ID': config.twitch_token
},
uri: 'https://api.twitch.tv/kraken/streams/' + channel,
method: 'GET'
}, function (err, res, body) {
answer = JSON.parse(body);
var isStreaming = answer.stream != null;
var toSend;
if(isStreaming){
toSend = {
"embed": {
"title": answer.stream.channel.display_name + " is streaming " + answer.stream.game.toUpperCase() + "!",
"description": answer.stream.channel.status,
"url": answer.stream.channel.url,
"color": 6570404,
"footer": {
"icon_url": "https://cdn0.iconfinder.com/data/icons/social-network-7/50/16-128.png",
"text": "Twitch.tv | " + Date(answer.stream.created_at)
},
"thumbnail": {
"url": answer.stream.channel.logo
},
"image": {
"url": answer.stream.preview.large
},
"video":{
"url": answer.stream.channel.url
},
"author": {
"name": "MarvinBot",
"url": "https://discordapp.com",
"icon_url": "https://raw.githubusercontent.com/hristiyanmarkov/MarvinBot/master/marvin/marvin.jpg"
},
"fields": [
{
"name": "Game",
"value": answer.stream.game,
"inline": true
},
{
"name": "Link",
"value": "[**Watch here**](" + answer.stream.channel.url + ")",
"inline": true
},
{
"name": "Viewers",
"value": answer.stream.viewers,
"inline": true
},
{
"name": "Followers",
"value": answer.stream.channel.followers,
"inline": true
}
]
}
}
}
else{
toSend = {
"embed": {
"title": channel + " is offline!",
"description": "They're not streaming currently.",
"url": "http://www.twitch.tv/" + channel,
"color": 6570404,
"footer": {
"icon_url": "https://cdn0.iconfinder.com/data/icons/social-network-7/50/16-128.png",
"text": "Twitch.tv"
},
"author": {
"name": "MarvinBot",
"url": "https://discordapp.com",
"icon_url": "https://raw.githubusercontent.com/hristiyanmarkov/MarvinBot/master/marvin/marvin.jpg"
}
}
}
}
message.channel.send(toSend).then(function (message) {
message.react(config.delemoji);
})
logger.info("got command !online");
});
}
};
// gives a random object from the list provided
if(message.content.startsWith(config.prefix + "random")) {
if(!args[0]){
message.channel.send("Nothing to randomize. 😬 ").then(function (message) {
message.react(config.delemoji);
});
}else{
var rand = args[Math.floor(Math.random() * args.length)];
message.channel.send("Randomizing....\nResult is: " + rand).then(function (message) {
message.react(config.delemoji);
});
logger.info("got command !random");
}
}
// sends a spam message to the user in private
if(message.content.startsWith(config.prefix + "spam")) {
message.author.send("Spam!").then(function (message) {
message.react(config.delemoji);
});
logger.info("got command !spam");
}
// protected command
// if(message.author.id !== config.ownerID) return;
// answers with pong!
if (message.content.startsWith(config.prefix + "ping")) {
message.channel.send("Ping! :ping_pong: Pong!").then(function (message) {
message.react(config.delemoji);
});
logger.info("got command !ping");
}
});
// listens for new members
bot.on('guildMemberAdd', member => {
// Send the message to a designated channel on a server:
const channel = member.guild.channels.find('name', 'general');
// Do nothing if the channel wasn't found on this server
if (!channel) return;
// Send the message, mentioning the member
channel.send('Welcome to the server, ' + member).then(function (message) {
message.react(config.delemoji);
logger.info("got new guild member");
});
});
//listen for new reaction
bot.on('messageReactionAdd', (reaction, user) => {
// logger.info("got new reaction");
// logger.info(reaction.emoji.identifier);
if(reaction.emoji == config.delemoji && reaction.count > 1 && reaction.me){
// logger.info("got new castro");
// wait a bit before deleting the message
reaction.message.delete(config.deltime);
}
});
bot.login(config.token);