Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@CWFranklin
Last active March 31, 2020 15:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save CWFranklin/591133837a3f1d4b5c7ec4da5d7c8ed8 to your computer and use it in GitHub Desktop.
Save CWFranklin/591133837a3f1d4b5c7ec4da5d7c8ed8 to your computer and use it in GitHub Desktop.
A script to detect user game changes and apply valid roles
var Discord = require('discord.js');
var discordBotToken = 'YOUR BOT TOKEN HERE';
var prefix = 'vores_';
var roleGame = {
"blackdesert": {
name: "black desert online",
id: ''
},
"lol": {
name: "league of legends",
id: ''
}
};
var client = new Discord.Client();
function collectRoleIds() {
client.guilds.forEach( function(server) {
server.roles.forEach( function(role) {
for (var gKey in roleGame) {
if (prefix + gKey === role.name) {
roleGame[gKey]['id'] = role.id;
}
}
});
});
}
function registeredGame(game) {
for (var gKey in roleGame) {
if ((game !== null && game.name !== undefined) && roleGame[gKey].name === game.name.toLowerCase()) {
return gKey;
}
}
return false;
}
client.on('message', msg => {
if ((msg.mentions.users.get(client.user.id) || !msg.channel.server) && msg.author !== client.user) {
var message = 'Supported Games (game / id):';
for (var gKey in roleGame) {
message += '\r\n' + roleGame[gKey].name + '/' + prefix + gKey;
}
msg.reply(message);
}
});
client.on('presenceUpdate', (oldUser, newUser) => {
if (oldUser.presence.game !== newUser.presence.game) {
if (oldUser.presence.game !== null && oldUser.presence.game !== null) {
var oldGame = registeredGame(oldUser.presence.game);
if (oldGame) {
newUser.removeRole(roleGame[oldGame].id, function (error) {
if (error) console.log(error);
});
}
}
if (newUser.presence !== null && newUser.presence.game !== null) {
var gameId = registeredGame(newUser.presence.game);
if (gameId) {
newUser.addRole(roleGame[gameId].id, function (error) {
if (error) console.log(error);
});
console.log(gameId);
}
}
}
});
client.on('ready', () => {
console.log('Bot serving. Setting status...');
client.user.setStatus('online');
client.user.setGame('Beating Down Chumps');
collectRoleIds();
});
client.login(discordBotToken);
@shoxie
Copy link

shoxie commented Feb 16, 2018

i found this from reddit. I wonder if what is ID in the very first line ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment