Browse Source

Add basic reaction role functionality that works

master
Abheek Dhawan 4 years ago
parent
commit
526dda8294
  1. 101
      bot.js

101
bot.js

@ -110,6 +110,8 @@ async function reactionRole(message) {
message.reply(`you don't have the correct permissions to create a reaction roles!`); message.reply(`you don't have the correct permissions to create a reaction roles!`);
return; return;
} }
let reactionsJSON = {};
let reactionsMessageJSON = {};
let splitMessage = message.content.split(" "); let splitMessage = message.content.split(" ");
if (splitMessage.length > 1) { if (splitMessage.length > 1) {
splitMessage.shift(); splitMessage.shift();
@ -117,6 +119,7 @@ async function reactionRole(message) {
message.channel.send(messageContent) message.channel.send(messageContent)
.then(reactionRoleMessage => { .then(reactionRoleMessage => {
const originalMessageAuthorId = message.author.id; const originalMessageAuthorId = message.author.id;
reactionRoleMessage.react("<a:anim_check:827985495295655988>");
message.delete(); message.delete();
const reactionFilter = (reaction, user) => { const reactionFilter = (reaction, user) => {
return user.id === originalMessageAuthorId; return user.id === originalMessageAuthorId;
@ -126,17 +129,41 @@ async function reactionRole(message) {
const messageFilter = response => { const messageFilter = response => {
return response; return response;
}; };
if (reaction.emoji.name === 'anim_check') {
reactionRoleMessage.delete();
message.channel.send(reactionRoleMessage.content)
.then(finalizedReactionRoleMessage => {
Object.keys(reactionsJSON).forEach(item => finalizedReactionRoleMessage.react(item));
reactionsMessageJSON[finalizedReactionRoleMessage.id] = reactionsJSON;
console.log(reactionsMessageJSON);
collection.findOne({ guild_id: message.guild.id }, (error, result) => {
if (error) {
console.error;
}
let reactionRoleMessages;
if (result.reaction_role_messages) {
reactionRoleMessages = result.reaction_role_messages;
collection.updateOne({ guild_id: message.guild.id }, { $push: { "reaction_role_messages": reactionsMessageJSON} });
} else {
collection.updateOne({ guild_id: message.guild.id }, { $set: { "reaction_role_messages": [reactionsMessageJSON]} });
}
});
message.author.dmChannel.send("Reaction role message confirmed!");
}).catch(console.error);
} else if (reaction.emoji.id) {
message.author.send(`Please refrain from using custom emojis!`)
} else {
message.author.send(`You reacted with ${reaction.emoji.name}. What role should this reaction give?`) message.author.send(`You reacted with ${reaction.emoji.name}. What role should this reaction give?`)
.then(() => { .then(() => {
message.author.dmChannel.awaitMessages(messageFilter, { max: 1, time: 30000, errors: ['time'] }) message.author.dmChannel.awaitMessages(messageFilter, { max: 1, time: 300000, errors: ['time'] })
.then(collected => { .then(collected => {
if (!collected.first().content) { if (!collected.first().content) {
collected.first().reply("query must contain at least 3 characters!") collected.first().reply("Query must contain at least 3 characters!")
return; return;
} }
if (collected.first().content.length < 3) { if (collected.first().content.length < 3) {
collected.first().reply("query must contain at least 3 characters!") collected.first().reply("Query must contain at least 3 characters!")
return; return;
} }
@ -144,14 +171,39 @@ async function reactionRole(message) {
let roleChannel; let roleChannel;
if (roles.array().length < 1) { if (roles.array().length < 1) {
collected.first().reply("no roles found with that name!"); collected.first().reply("No roles found with that name!");
return; return;
} }
const role = roles.array()[0]; const role = roles.array()[0];
console.log(role.name);
const verificationEmbed = new Discord.MessageEmbed()
.setTitle(`Are you sure you would like ${reaction.emoji.name} to correspond to the **${role.name}** role?`)
.setDescription("React to this message to verify")
.setThumbnail(message.guild.iconURL())
.setColor("fda172")
.setTimestamp();
message.author.dmChannel.send(verificationEmbed)
.then(verificationEmbed => {
verificationEmbed.react('<a:anim_check:827985495295655988>');
verificationEmbed.react('<a:anim_cross:827990162113560576>');
const roleConfirmationFilter = (reaction, user) => {
return ['anim_check', 'anim_cross'].includes(reaction.emoji.name) && !user.bot;
};
verificationEmbed.awaitReactions(roleConfirmationFilter, { max: 1 })
.then(userReaction => {
const roleConfirmationReaction = userReaction.first();
if (roleConfirmationReaction.emoji.name === 'anim_check') {
reactionsJSON[reaction.emoji.name] = role.id;
message.author.dmChannel.send(`Role \`${role.name}\` used`)
} else {
message.author.dmChannel.send("If you would like to select a different role, please re-react to the message in the server");
}
}).catch(console.error);
}).catch(console.error);
}).catch(console.error); }).catch(console.error);
}); });
}
}); });
}); });
} else { } else {
@ -1057,12 +1109,29 @@ client.on('messageReactionAdd', (messageReaction, user) => {
kulboardEmbed.addField("Message", message.content); kulboardEmbed.addField("Message", message.content);
} }
} }
if (messageReaction.message.guild) {
collection.findOne({ guild_id: messageReaction.message.guild.id }, (error, result) => { collection.findOne({ guild_id: messageReaction.message.guild.id }, (error, result) => {
let kulboardChannel; let kulboardChannel;
let botLogsChannel; let botLogsChannel;
let reactionRoleMessages;
if (error) { if (error) {
console.error; console.error;
} }
if (result.reaction_role_messages) {
reactionRoleMessages = result.reaction_role_messages;
reactionRoleMessages.forEach(async item => {
if (item.hasOwnProperty(messageReaction.message.id)) {
if (item[messageReaction.message.id].hasOwnProperty(messageReaction.emoji.name)) {
//console.log(messageReaction.message.guild.roles.fetch(item[messageReaction.message.id][messageReaction.emoji.name]));
//if (messageReaction.message.guild.roles.cache.get(item[messageReaction.message.id][messageReaction.emoji.name])
const member = await messageReaction.message.guild.members.fetch(user.id);
if (!member.user.bot) {
member.roles.add(item[messageReaction.message.id][messageReaction.emoji.name]).catch();
}
}
}
});
}
if (result.bot_logs_id) { if (result.bot_logs_id) {
botLogsChannel = result.bot_logs_id; botLogsChannel = result.bot_logs_id;
if (message.guild.channels.cache.get(botLogsChannel)) { if (message.guild.channels.cache.get(botLogsChannel)) {
@ -1098,6 +1167,7 @@ client.on('messageReactionAdd', (messageReaction, user) => {
} }
} }
}); });
}
}); });
}); });
@ -1166,13 +1236,31 @@ client.on('messageReactionRemove', async (messageReaction, user) => {
// if (message.content) { // if (message.content) {
// kulboardEmbed.addField("Message", message.content) // kulboardEmbed.addField("Message", message.content)
// } // }
if (messageReaction.message.guild) {
collection.findOne({ guild_id: messageReaction.message.guild.id }, (error, result) => { collection.findOne({ guild_id: messageReaction.message.guild.id }, (error, result) => {
let kulboardChannel; let kulboardChannel;
let botLogsChannel; let botLogsChannel;
let reactionRoleMessages;
if (error) { if (error) {
console.error; console.error;
} }
if (result.reaction_role_messages) {
reactionRoleMessages = result.reaction_role_messages;
reactionRoleMessages.forEach(async item => {
if (item.hasOwnProperty(messageReaction.message.id)) {
if (item[messageReaction.message.id].hasOwnProperty(messageReaction.emoji.name)) {
//console.log(messageReaction.message.guild.roles.fetch(item[messageReaction.message.id][messageReaction.emoji.name]));
//if (messageReaction.message.guild.roles.cache.get(item[messageReaction.message.id][messageReaction.emoji.name])
const member = await messageReaction.message.guild.members.fetch(user.id);
if (!member.user.bot) {
if (member.roles.cache.has(item[messageReaction.message.id][messageReaction.emoji.name])) {
member.roles.remove(item[messageReaction.message.id][messageReaction.emoji.name]).catch();
}
}
}
}
});
}
if (result.bot_logs_id) { if (result.bot_logs_id) {
botLogsChannel = result.bot_logs_id; botLogsChannel = result.bot_logs_id;
if (message.guild.channels.cache.get(botLogsChannel)) { if (message.guild.channels.cache.get(botLogsChannel)) {
@ -1208,6 +1296,7 @@ client.on('messageReactionRemove', async (messageReaction, user) => {
} }
} }
}); });
}
}); });
}); });

Loading…
Cancel
Save