forked from enteryournamehere/wilson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwilson.js
242 lines (208 loc) · 7.45 KB
/
wilson.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
const secure = require('./secure.json');
const path = require('path');
const SequelizeProvider = require('./utils/Sequelize');
const database = require('./utils/database.js');
const updates = require('./models/updates.js');
const { Wilson } = require('./utils/wilson');
const translation = require('./events/translation.js');
const sticky = require('./events/sticky.js');
const ideaVault = require('./events/idea-vault');
const scamlink = require('./events/scamlink');
const collaborators = require('./events/collaborators');
const Webhook = require('./utils/webhook.js');
Wilson.on('debug', console.log);
Wilson.registry
.registerGroups([
['config', 'Config commands'],
['fun', 'Fun commands'],
['ideavault', 'Idea vault commands'],
['mod', 'Mod commands'],
['owner', 'Owner commands'],
['collaborators', 'Collaborator commands'],
])
.registerDefaultTypes()
.registerDefaultGroups()
.registerDefaultCommands({
ping: false,
prefix: false,
unknownCommand: false,
})
.registerCommandsIn(path.join(__dirname, 'commands'));
Wilson.on('ready', async () => {
const guild = await Wilson.guilds.fetch(secure.updateguild);
// If this isn't run at least once, some Discord.js commands don't work or throw exceptions.
await guild.roles.fetch();
await guild.members.fetch();
console.log(`{green}Ready!`);
});
Wilson.on('error', (msg) => {
console.log('{red}Error!{reset}', msg);
});
database.start();
Wilson.setProvider(new SequelizeProvider(database.db)).catch(console.error);
// Events
Wilson.on('message', translation);
Wilson.on('message', sticky);
Wilson.on('message', scamlink);
// subscribe the idea vault's events
Wilson.on('channelUpdate', ideaVault.channelUpdate);
Wilson.on('messageReactionAdd', ideaVault.messageReactionAdd);
Wilson.on('messageReactionRemove', ideaVault.messageReactionRemove);
Wilson.on('messageUpdate', ideaVault.messageUpdate);
Wilson.on('ready', ideaVault.readyFactory(Wilson));
// subscribe the collaborators events
Wilson.on('guildMemberUpdate', collaborators.guildMemberUpdate);
Wilson.on('roleUpdate', collaborators.guildRoleUpdate);
Wilson.on('roleDelete', collaborators.guildRoleDelete);
Wilson.dispatcher.addInhibitor(msg => {
if (msg.webhookID) return 'nope';
return false;
});
Wilson.on('message', (msg) => {
if (!msg.author) return;
if (!msg.guild) return;
if (!msg.content.match(/https?:\/\//)) return;
msg.guild.members.fetch(msg.author).then((member) => {
const beenHereMinutes = (Date.now() - member.joinedTimestamp) / 1000 / 60;
if (beenHereMinutes < 10) {
msg.reply('welcome to the Wintergatan Discord server! To prevent spam and bots, there is a 10 minute wait time before new members can send links, so please try again in a moment. Thank you!').then(reply => {
setTimeout(() => {
reply.delete();
}, 60 * 1000);
});
msg.delete();
}
});
});
// guild ids for role transfers
/**
* @typedef {String} oldRoleId
* @typedef {String} newRoleId
*/
const oldGuildId = '413366614747250708';
const newGuildId = '649165975647682560';
/**
* map for role transfers from old server member to new server
* @type {Map<oldRoleId, newRoleId>}
*/
const roleMap = new Map();
roleMap.set('413371714827714560', '649189528958926887'); // admin
roleMap.set('413371743416352768', '649189529231556609'); // moderator
roleMap.set('453171669004058625', '650660087731585111'); // youtube
roleMap.set('609714412554682378', '650660149857615873'); // twitch
roleMap.set('453171752667709441', '650660135894646804'); // twitter
roleMap.set('465044582569082881', '708972931430088715'); // community
roleMap.set('465043327201443841', '708988136398651482'); // minecraft
roleMap.set('418726756501946368', '650707449610895362'); // artist
roleMap.set('418683780476174336', '651548732696821776'); // craftsman
roleMap.set('418683680567853056', '651548761092259891'); // developer
roleMap.set('418683762797314058', '651548957322903553'); // engineer
roleMap.set('418718491781365761', '651548806533349426'); // gamer
roleMap.set('418683641850232833', '651548778746216448'); // GFX designer
roleMap.set('418726789444272129', '651548709527617557'); // musician
roleMap.set('694608130629435432', '709469819874836541'); // john
const mmxTeamRoleId = '650660016755310592';
const ccAgreedRoleId = '709386821850759178';
const mmxTeamCcAgreedRoleId = '709406460450177094';
Wilson.on('guildMemberAdd', member => {
/**
* transfer roles from old server member to new server member
*/
if (member.guild.id != newGuildId) return; // if join is not in new server, return
const oldMember = Wilson.guilds.resolve(oldGuildId).members.resolve(member.id);
if (!oldMember) return; // if user is not in old server, return
oldMember.roles.cache.forEach((v, k) => {
const newId = roleMap.get(k);
if (newId) member.roles.add(newId); // if role is in map, add role
});
});
Wilson.on('guildMemberUpdate', (oldMember, newMember) => {
// if the roles have changed, do stuff
if (oldMember.roles.cache.keys() != newMember.roles.cache.keys()) {
const roles = newMember.roles.cache.keyArray(); // get updated roles
// if member has both roles and not the combined role, add combined role
if (roles.includes(mmxTeamRoleId) && roles.includes(ccAgreedRoleId) && !roles.includes(mmxTeamCcAgreedRoleId)) {
newMember.roles.add(mmxTeamCcAgreedRoleId);
// if member does not have both roles but has combined role, remove combined role
} else if ((!roles.includes(mmxTeamRoleId) || !roles.includes(ccAgreedRoleId)) && roles.includes(mmxTeamCcAgreedRoleId)) {
newMember.roles.remove(mmxTeamCcAgreedRoleId);
};
}
});
const updatesConfig = {
guild: secure.updateguild,
roles: secure.roles,
webhooks: secure.webhooks,
};
const twitterWebhook = new Webhook(updatesConfig.webhooks.twitter);
const youtubeWebhook = new Webhook(updatesConfig.webhooks.youtube);
function twitter2Fetch() {
Wilson.fetches.twitter.last = Date.now();
require('./utils/twitter2.js').fetch().then((x) => {
if (!x.new) return;
twitterWebhook.send(`<@&${updatesConfig.roles.twitter}>`, x.embed).catch(() => { }).then(() => {
updates.addUpdate(
'twitter',
x.postid,
x.time,
);
});
}).catch(e => console.log(e)).then(() => {
setTimeout(twitter2Fetch, 10 * 60 * 1000);
});
}
function youtube2Fetch() {
Wilson.fetches.youtube.last = Date.now();
require('./utils/youtube2.js').fetch().then((x) => {
if (!x.new) return;
youtubeWebhook.send(`<@&${updatesConfig.roles.youtube}>`, x.embed).catch(() => { }).then(() => {
updates.addUpdate(
'youtube',
x.postid,
x.time,
);
});
}).catch(e => console.log(e)).then(() => {
setTimeout(youtube2Fetch, 5 * 60 * 1000);
});
}
Wilson.fetches = {
youtube: {
run: youtube2Fetch,
last: 0,
},
twitter: {
run: twitter2Fetch,
last: 0,
},
};
Wilson.once('ready', () => {
if (secure.fetches.youtube) Wilson.fetches.youtube.run();
if (secure.fetches.twitter) Wilson.fetches.twitter.run();
});
const colours = {
black: '\x1b[30m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
cyan: '\x1b[36m',
white: '\x1b[37m',
reset: '\x1b[0m',
};
const oldLog = console.log;
global.console.log = function(...args) {
args = args.map(arg => {
if (typeof arg === 'string') {
/* eslint-disable guard-for-in */
for (const colour in colours) {
arg = arg.replace('{' + colour + '}', colours[colour]);
}
arg += colours.reset;
}
return arg;
});
return oldLog(new Date().toISOString().replace('T', ' ').replace('Z', ''), ...args);
};
Wilson.login(secure.token);