This repository has been archived by the owner on May 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
146 lines (111 loc) · 4.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
// Discord bot needs
const Discord = require('discord.js');
const client = new Discord.Client();
// Filesystem needs
const fs = require('fs');
const util = require('util');
const date = new Date();
// NOTE: Feel free to edit or change most of the const values to your needs.
/* --- Token --- */
// Super secret token!
const token = "token goes here";
/* --- Logging --- */
// Disable logging to a file here
const log_to_file = true;
// Create log file
var log_file;
if (log_to_file) {
var datetime = "-" + (date.getMonth() + 1) + "-" + date.getDate() + "-" + date.getHours() + "-" + date.getMinutes() + "-" + date.getSeconds();
log_file = fs.createWriteStream(__dirname + '/log' + datetime + '.log', {flags : 'w'});
} else {
console.log("[!] No logging to file!");
}
// Override log to also write to file
console.log = function(msg) {
if ( log_to_file ) { log_file.write(util.format(msg) + '\n'); }
process.stdout.write(util.format(msg) + '\n');
};
/* --- Content Pairs --- */
// Initialize data
var copypastaData;
var tyroneData;
// Dict of trigger : response pairs
// This can be anything, from specific text, to video links, to emojis, to whatever.
// This is loaded in from data.json
var contentPairs = {};
// Load data from json and apply it
function loadContentPairs() {
/// Read
// get copypasta data from copypasta.json
contentData = JSON.parse(fs.readFileSync("copypasta.json"));
/// Parse
// Load the copypasta pairs
console.log('[*] Loading copypasta pairs...');
// Load data into content pairs from jsonData
for(var attribute in contentData){
contentPairs[attribute] = contentData[attribute];
}
}
/* --- Help message setup --- */
// Length of help output for responses
const responseLen = 40;
// Help message loaded on start
var helpMsg = "";
// This is the command the user must enter to get the help message from the bot
const helpCommand = "!bot.help";
/* --- Ready up --- */
// Ready message
// From the docs:
// 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', () => {
// Load data from json
loadContentPairs();
// Load the help message
// This creates a specfic message to send to users who request to see the
// bot features.
console.log('[*] Loading help...');
for (var key in contentPairs) {
helpMsg += (key + " => " + contentPairs[key].substring(0, responseLen) + "...\n")
}
// Set the bot's status. Can be anything, but it is a user friendly
// idea to set it to the command used to get help
client.user.setStatus('online', helpCommand);
console.log('[*] Logged in & monitoring');
});
/* --- Main event listener --- */
// Event listener for messages
client.on('message', message => {
console.log('[:] Msg found...');
// Ignore messages from bots. Serves to not create endless loops if responds to self for example.
if (message.author.bot) {
console.log('[:] Ignorng found as it is a bot msg...');
} else {
// Iterate through dict, look for triggers
console.log('[:] Searching for match...');
// Search for a match
for (var key in contentPairs) {
// If a trigger is found, put response into channel
if (message.content.indexOf(key) !== -1) {
console.log("[!] Message found!")
message.channel.sendMessage(contentPairs[key]);
}
}
if (message.content.indexOf(helpCommand) !== -1) {
console.log("[:] Help requested.")
message.channel.sendMessage(message.author + " I PM'd you a list of my commands");
console.log("[:] Help sent.")
message.author.sendMessage(helpMsg).catch(error=>{});
}
// Alterative methods may be used as well
// for example, if a message is just 'RMS' it'll respond with the GNU/Linux interjection
// It is recommended to just make two entries in data.json instead, if you really need it
if (message.content === "RMS") {
console.log("[!] Alternative message found!")
// As you can see, we can just send out contentPairs with a key to get the message
message.channel.sendMessage(contentPairs["linux"]);
}
}
});
// Login
client.login(token);