-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponseBot.js
More file actions
120 lines (96 loc) · 3.01 KB
/
responseBot.js
File metadata and controls
120 lines (96 loc) · 3.01 KB
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
//import discord.js
const Discord = require('discord.js');
//create instance of discord client
const client = new Discord.Client();
//Gets configuration information from discordConfig.json
const config = require('./discordConfig.json');
//Token for bot
const token = config.token;
//ID of Admin
const adminID = config.adminID;
//Command prefix
const prefix = config.prefix;
// The ready event is vital, it means that your bot will only start reacting to information
// from Discord _after_ ready is emitted.
client.on('ready', function(message) {
console.log('ResponseBot is ready!');
});
//Log our bot in
client.login(token, output);
function output(error, token) {
if (error) {
console.log('There was an error logging in: ${error}');
return;
} else
console.log('Logged in. Token: ${token}');
}
//Bot listens for a message of a particular format and responds
client.on('message', function(message) {
if (message.author.bot) return;
var text = message.content;
if (text.startsWith(prefix)) {
processCommand(message);
} else if (text.startsWith('~') && message.author.id === adminID) {
processAdminCommand(message);
} else {
processNonCommand(message);
}
checkForPalindrome(message);
message.channel.stopTyping(true);
});
var commands = require('./commands.js');
var commandNames = require('./commandNames');
commands.functions.setClient(client);
//Process commands from users
function processCommand(message) {
var text = message.content.toLowerCase();
var command = text.split(" ")[0];
if (commandNames[command]) {
commands.functions[commandNames[command]](message);
}
}
//JSON object that contians generic responses to specific messages
var responses = require('./responses.json');
//Process noncommands from anyone
function processNonCommand(message) {
var text = message.content;
if (responses[text]) {
sendMessage(message, responses[text]);
}
}
//Processes the commands by the admin based on adminID in discordConfig.json
function processAdminCommand(message) {
var text = message.content;
var mentions = message.mentions.users.array();
if (text.startsWith("~" + "prefix")) {
var arg = text.split(" ");
config.prefix = arg[1];
prefix = config.prefix;
}
}
function sendMessage(message, text) {
message.channel.startTyping();
client.setTimeout(function() {
message.channel.send(text);
}, 1000);
message.channel.stopTyping(true);
}
/*
*
* Logic Functions
*
*/
function checkForPalindrome(message) {
var text = message.content;
if (text.includes('\n')) return;
var responseText = text.replace(/[^\w\d\s]+/g, ""); //replaces all characters except word and whitespace characters
var evalText = responseText.replace(/ /g, "").toLowerCase(); //replaces all spaces with empty characters
var length = evalText.length;
if (length < 7) return;
for (var i = 0; i < (length / 2); i++) {
if (evalText.charAt(i) != evalText.charAt(length - i - 1)) {
return;
}
}
sendMessage(message, "'" + responseText + "'" + " is a palindrome!");
}