forked from Clemson-Esports/Clemson-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
136 lines (132 loc) · 5.44 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
const config = require('./config.json')
const path = require('path');
const {
CommandoClient
} = require('discord.js-commando');
const client = new CommandoClient({
commandPrefix: config.prefix,
owner: config.owner
});
client
.on('error', console.error)
.on('warn', console.warn)
.on('debug', console.log)
.on('ready', () => {
console.log(`Client ready; logged in as ${client.user.username}#${client.user.discriminator} (${client.user.id})`);
client.user.setActivity(`[${config.prefix} help]`, {
type: 'PLAYING'
})
.then(presence => console.log(`Activity set to ${presence.activities[0].name}`))
.catch(console.error);
})
.on('disconnect', () => {
console.warn('Disconnected!');
})
.on('reconnecting', () => {
console.warn('Reconnecting...');
})
.on('commandError', (cmd, err) => {
console.error(`Error in command ${cmd.groupID}:${cmd.memberName}`, err);
})
.on('commandBlocked', (msg, reason) => {
console.log(oneLine `
Command ${msg.command ? `${msg.command.groupID}:${msg.command.memberName}` : ''}
blocked; ${reason}
`);
})
.on('commandPrefixChange', (guild, prefix) => {
console.log(oneLine `
Prefix ${prefix === '' ? 'removed' : `changed to ${prefix || 'the default'}`}
${guild ? `in guild ${guild.name} (${guild.id})` : 'globally'}.
`);
})
.on('commandStatusChange', (guild, command, enabled) => {
console.log(oneLine `
Command ${command.groupID}:${command.memberName}
${enabled ? 'enabled' : 'disabled'}
${guild ? `in guild ${guild.name} (${guild.id})` : 'globally'}.
`);
})
.on('groupStatusChange', (guild, group, enabled) => {
console.log(oneLine `
Group ${group.id}
${enabled ? 'enabled' : 'disabled'}
${guild ? `in guild ${guild.name} (${guild.id})` : 'globally'}.
`);
})
.on("presenceUpdate", function(oldPresence, newPresence){
streamerRoles(oldPresence, newPresence);
});
// To add a new command group just add a new entry in the registerGroups call
client.registry
.registerGroups([
['utility', 'Moderation and Utility commands'],
['clemson', 'Commands that relate to Clemson!'],
['esports', 'Esports related commands'],
['fun', 'Some random stuff, just for fun!'],
['lol', 'League of Legends'],
['mc', 'Minecraft'],
['misc', 'Miscellaneous commands'],
['templates', 'For testing/dev team'],
])
.registerDefaults()
.registerCommandsIn(path.join(__dirname, 'commands'));
client.login(config.token);
function streamerRoles(oldPresence, newPresence) {
streaming_role = newPresence.guild.roles.cache.find(role => role.name === 'CURRENTLY STREAMING')
clemson_role = newPresence.guild.roles.cache.find(role => role.name === 'Clemson')
// check if server has the role
if (streaming_role && clemson_role) {
// make sure user status/activities is not empty array
if (newPresence.activities.length !== 0) {
// check for streaming status
if (newPresence.activities[0].name === 'Twitch' && newPresence.activities[0].type === 'STREAMING') {
newPresence.guild.members.fetch(newPresence.userID)
.then(user => {
// make sure user has clemson role
if (user.roles.cache.has(clemson_role.id)) {
user.roles.add(streaming_role)
.catch(err => {
if (err.code === 'INVALID_TYPE') {
console.log(`${user.displayName} already has streaming role`)
} else {
console.log(err)
}
});
}
})
}
// when user turn off stream but keep game on
else if (newPresence.activities[0].name !== 'Twitch' && newPresence.activities[0].type !== 'STREAMING') {
newPresence.guild.members.fetch(newPresence.userID)
.then(user => {
if (user.roles.cache.has(streaming_role.id)) {
user.roles.remove(streaming_role)
.catch(err => {
if (err.code === 'INVALID_TYPE') {
console.log(`${user.displayName}'s role already removed`)
} else {
console.log(err)
}
});
}
})
}
} else {
// when user turn off stream and game
newPresence.guild.members.fetch(newPresence.userID)
.then(user => {
if (user.roles.cache.has(streaming_role.id)) {
user.roles.remove(streaming_role)
.catch(err => {
if (err.code === 'INVALID_TYPE') {
console.log(`${user.displayName}'s role already removed`)
} else {
console.log(err)
}
});
}
})
}
}
}