-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
141 lines (109 loc) · 4.29 KB
/
Copy pathindex.js
File metadata and controls
141 lines (109 loc) · 4.29 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const fs = require('fs');
const config = require('./config.json');
const Discord = require('discord.js');
const format = require('./message-formatter.js');
// create a new client
const client = new Discord.Client();
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
// fetch commands FROM files
for (const file of commandFiles) {
// fetch command object
const command = require(`./commands/${file}`);
// set command object to a command slot in the client
client.commands.set(command.name, command);
}
// when client is ready, run this
client.once('ready', () => {
console.log('Ready!');
});
// updates to client on message
client.on('message', message => {
if(!message.content.startsWith(config.prefix) || message.author.bot) return;
const args = message.content.slice(config.prefix.length).split(/ +/);
const commandName = args.shift().toLowerCase();
const command = client.commands.get(commandName)
|| client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
if (!command) return;
// check to make sure it's not an event/watch command
if ((command.type === "eventlist" || command.type === "watchlist") && message.channel.type === 'dm') {
return message.reply('I can\'t execute that command inside DMs!');
}
// check command's arg req
if(command.args && !args.length) {
return message.channel.send(`You didn't provide any arguments, ${message.author}!`);
}
try {
console.log(message.content);
command.execute(message, args);
} catch (error) {
console.error(error);
message.reply('there was an error trying to execute that command');
return;
}
// after running event command, update the event message
if(command.type === "eventlist") {
// scan server-info.json to find server ID
console.log('Reading the server-info file');
let raw = fs.readFileSync(`./resources/server-info.json`);
let serverInfo = JSON.parse(raw);
console.log(serverInfo);
for(let i = 0; i < serverInfo.servers.length; i++) {
// find server ID in server info
if(serverInfo.servers[i].id === message.guild.id) {
console.log(`${message.guild.id} was found in server-info`);
// grab event channel id & event message id
let eventChannelId = serverInfo.servers[i].eventChannelId;
console.log(`The event channel id is: ${eventChannelId}`);
let eventMessageId = serverInfo.servers[i].eventMessageId;
console.log(`The event message id is: ${eventChannelId}`);
// grab event channel itself
let eventChannel = client.channels.cache.get(eventChannelId);
console.log(`Grabbed event channel: ${eventChannel}`);
// check null
if(eventChannel) {
// fetch message using ID
console.log('Editing message');
eventChannel.messages.fetch(eventMessageId)
.then(message => message.edit(format.eventlistMessage(message.guild.id)))
.catch(console.error);
}
return;
}
}
}
// after running the show command, update the show message
// TO DO: UPDATE USING METHOD IN EVENT MESSAGE
if(command.type === "watchlist") {
// scan server-info.json to find server ID
console.log('Reading the server-info file');
let raw = fs.readFileSync(`./resources/server-info.json`);
let serverInfo = JSON.parse(raw);
console.log(serverInfo);
for(let i = 0; i < serverInfo.servers.length; i++) {
// find server ID in server info
if(serverInfo.servers[i].id === message.guild.id) {
console.log(`${message.guild.id} was found in server-info`);
// grab watch channel id & watch message id
let watchChannelId = serverInfo.servers[i].watchChannelId;
console.log(`The watch channel id is: ${watchChannelId}`);
let watchMessageId = serverInfo.servers[i].watchMessageId;
console.log(`The watch message id is: ${watchMessageId}`);
// grab watch channel itself
let watchChannel = client.channels.cache.get(watchChannelId);
console.log(`Grabbed watch channel: ${watchChannel}`);
// check null
if(watchChannel) {
// fetch message using ID
console.log('Editing message');
watchChannel.messages.fetch(watchMessageId)
.then(message => message.edit(format.watchlistMessage(message.guild.id)))
.catch(console.error);
}
return;
}
}
}
});
// log in to discord
client.login(config.token);