Skip to content

Instantly share code, notes, and snippets.

@Wist9063
Forked from fethib/app.js
Last active December 8, 2017 04:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Wist9063/e342e75be6e1394ee47b4d05634ac002 to your computer and use it in GitHub Desktop.
Save Wist9063/e342e75be6e1394ee47b4d05634ac002 to your computer and use it in GitHub Desktop.
const Discord = require('discord.js'); // remember to install discord.js via: npm install discord.js
const bot = new Discord.Client();
const token = "TOKEN" // to get your token go here: https://discordapp.com/developers/applications/me/
// don't share the token with anyone.
// 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 member
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);
});
}
/*
You've gotta make the role below.
The bot dosen't make it by its self
If the role is not there, it won't assign it.
*/
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)}
});
}
});
bot.login(token);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment