BlueGit

blue-star danique-bot

Files

danique-bot / bot.js

bot.js

Import template
timfa Tim · pushed by timfa · 4 years ago
c788ad20 History
bot.js 5.7 KiB Download Raw
1var Discord = require('discord.io');
2var auth = require('./auth.json');
3var fs = require('fs');
4
5var cmdsFile = require('./commands.js');
6var commands = cmdsFile.cmds;
7var admins = cmdsFile.admins;
8console.log(commands);
9
10var bot = new Discord.Client({
11 token: auth.token,
12 autorun: true
13});
14
15console.log("initializing...");
16
17var initialized = false;
18bot.commands = commands;
19
20bot.data = null;
21bot.lastSave = Date.now();
22
23bot.saveBackup = function(silent = false)
24{
25 fs.writeFile("./botDataBackup.json", JSON.stringify(bot.data), function(err) {
26 if (err) {
27 console.log(err);
28 bot.sendMessage({
29 to: bot.data.lastServer,
30 message: "Failed to create a back-up.",
31 typing: false
32 });
33 }
34 else
35 {
36 if(!silent)
37 {
38 bot.sendMessage({
39 to: bot.data.lastServer,
40 message: "Bot data back-up created.",
41 typing: false
42 });
43 }
44 }
45 });
46}
47
48bot.saveData = function(callback, context)
49{
50 console.log("Saving...");
51 try
52 {
53 if(bot.data != null && bot.data != {} && initialized)
54 {
55 bot.data = bot.data || {};
56
57 fs.writeFile("./botData.json", JSON.stringify(bot.data), function(err) {
58 if (err) {
59 console.log(err);
60 }
61
62 context = context || this;
63 callback && callback.call(context, true);
64 });
65 }
66 }
67 catch(ex)
68 {
69 console.log("Error while saving!");
70 console.log(ex);
71 context = context || this;
72 callback && callback.call(context, false);
73 }
74}
75
76console.log("savedata function defined")
77
78bot.on('ready', function (evt) {
79 console.log('Connected!');
80 console.log('Logged in as: ');
81 console.log(bot.username + ' - (' + bot.id + ')');
82
83 fs.readFile('./botData.json', function read(err, data)
84 {
85 if (err)
86 {
87 data = "{}";
88 }
89 try
90 {
91 bot.data = JSON.parse(data);
92 }
93 catch(e)
94 {
95 if(bot.data == null)
96 {
97 console.log("Memory broken, will attempt to restore backup.");
98 }
99 }
100
101 if(bot.data == null || bot.data == {})
102 {
103 fs.readFile('./botDataBackup.json', function read(err, data) {
104 if (err) {
105 data = "{}";
106 }
107 try
108 {
109 bot.data = JSON.parse(data);
110 console.log("Backup restored");
111
112 if(bot.data.lastServer)
113 {
114 bot.sendMessage({
115 to: bot.data.lastServer,
116 message: "A crash occurred, but I managed to restore a backup of my memory.",
117 typing: false
118 });
119 saveBackup = false;
120 }
121 }
122 catch(e)
123 {
124 if(bot.data == null)
125 {
126 bot.data = {};
127 console.log("Memory backup broken, making new");
128 saveBackup = false;
129
130 if(bot.data.lastServer)
131 {
132 bot.sendMessage({
133 to: bot.data.lastServer,
134 message: "A crash occurred, and my memory file was lost :(",
135 typing: false
136 });
137 }
138 }
139 }
140 initialized = true;
141 });
142 }
143 else //successfully loaded
144 {
145 bot.saveBackup(true);
146
147 initialized = true;
148 }
149
150 bot.data = bot.data || {};
151
152 bot.data.servers = bot.data.servers || [];
153
154 if(bot.data.update && bot.data.update != "no")
155 {
156 bot.sendMessage({
157 to: bot.data.update,
158 message: "Updated!",
159 typing: false
160 });
161
162 bot.data.update = "no";
163 }
164 else
165 {
166 console.log("no update notifiy to do");
167 }
168 });
169});
170
171console.log("onready defined")
172
173bot.on('message', function (user, userID, channelID, message, evt) {
174 console.log(message);
175 // Our bot needs to know if it will execute a command
176 // It will listen for messages that will start with `!`
177
178 if(!initialized)
179 return;
180
181 var wasBot = evt.d.author.bot;
182
183 var id = evt.d.guild_id || userID;
184
185 if(bot.data.servers.indexOf(id) < 0)
186 {
187 bot.data.servers.push(id);
188 }
189
190 var group = [userID];
191
192 var words = message.split(' ');
193 bot.data[id] = bot.data[id] || {};
194
195 if(Date.now() > bot.lastSave + 3600000) // one hour
196 {
197 bot.saveBackup(true);
198 bot.lastSave = Date.now();
199 }
200
201 if (message.substring(0, 1) == '$')
202 {
203 var args = message.substring(1).split(' ');
204 var cmd = args[0];
205
206 var info = {user:user, userID:userID, channelID:channelID, message:message, evt:evt, serverId:id};
207
208 bot.data.lastServer = channelID;
209
210 args = args.splice(1);
211 for(var i = 0; i < commands.length; i++)
212 {
213 if(cmd.toLowerCase() == commands[i].cmd)
214 {
215 if(commands[i].hidden && admins.indexOf(info.userID) == -1)
216 return;
217
218 commands[i].execute(bot, info, args);
219 break;
220 }
221 }
222 }
223
224 bot.saveData();
225});
226
227console.log("onmessage defined")