Skip to content

Instantly share code, notes, and snippets.

@fethib
Last active February 7, 2018 02:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save fethib/0822aa3a4a07286a8496825f40b526b3 to your computer and use it in GitHub Desktop.
Save fethib/0822aa3a4a07286a8496825f40b526b3 to your computer and use it in GitHub Desktop.
const Discord = require('discord.js');
const bot = new Discord.Client();
// Connexion to Discord API
bot.login(TOKEN);
// Bot connexion
bot.on("ready", function () {
console.log("The bot is online !");
// Removes every game roles when the bot logs in
bot.guilds.forEach(function(guild) { // For each guild
guild.members.forEach(function(member) { // For each membre
member.roles.forEach(function(role) { // For each role
if(role.name.startsWith("🎮 ")) {member.removeRole(role)} // If the role name starts with a controller, removes it
});
});
});
});
// Bot disconnection
bot.on("disconnect", function () {
console.log("Disconnected...");
});
// Feature : The bot gives members a role depending the game they are currently playing
bot.on("presenceUpdate", (oldMember, newMember) => {
var guild = newMember.guild;
if (newMember.user.presence.game) {
// Removes every previous game roles
newMember.roles.forEach(function(role) {
if(role.name.startsWith("🎮 ")) {newMember.removeRole(role)}
});
var gamename = newMember.user.presence.game.name;
if (gamename.startsWith("Counter-Strike")) { // EX : Specific to Counter-Strike : Global Offensive, because it's too long to create a role with this name
newMember.addRole(guild.roles.find("name","🎮 Counter-Strike"))
.then(function() {
console.log(`Role : Counter-Strike given to ${newMember.user.username}`);
},function(e) {
console.error(e);
});
}
else { // If a role "🎮 [name]" exists, gives it
var newrole = guild.roles.find("name",`🎮 ${gamename}`);
if (newrole) {
newMember.addRole(guild.roles.find("name",`🎮 ${gamename}`))
.then(function() {
console.log(`Role : ${gamename} given to ${newMember.user.username}`);
},function(e) {
console.error(e);
});
}
}
}
else if (!newMember.user.presence.game) {
newMember.roles.forEach(function(role) {
if(role.name.startsWith("🎮 ")) {newMember.removeRole(role)}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment