added reactions + reaction controll command (it can assign all types and setup messages)

This commit is contained in:
2022-12-28 22:28:50 +01:00
parent d1e26ac9bb
commit 16dac26a2c
5 changed files with 173 additions and 54 deletions

View File

@ -9,7 +9,6 @@ module.exports = {
CommandCreator() { CommandCreator() {
const data = new SlashCommandBuilder() const data = new SlashCommandBuilder()
.setName(this.name) .setName(this.name)
.setDefaultMemberPermissions(PermissionFlagsBits.KickMembers)
.setDescription(this.description) .setDescription(this.description)
.setDefaultMemberPermissions(PermissionFlagsBits.MuteMembers) .setDefaultMemberPermissions(PermissionFlagsBits.MuteMembers)
.addMentionableOption(option => option.setName("target").setDescription("target to mute").setRequired(true)) .addMentionableOption(option => option.setName("target").setDescription("target to mute").setRequired(true))

58
Commands/reaction.js Normal file
View File

@ -0,0 +1,58 @@
const { EmbedBuilder, SlashCommandBuilder, Client, MessageInteraction,PermissionFlagsBits, ButtonStyle } = require('discord.js');
module.exports = {
name: "reaction",
description: "setup command for reaction type messages",
options: [],
CommandCreator() {
const data = new SlashCommandBuilder()
.setName(this.name)
.setDescription(this.description)
.setDefaultMemberPermissions(PermissionFlagsBits.MuteMembers)
.addStringOption(option => option.setName("type").setDescription("type of action to perform on reaction").setRequired(true).addChoices(
{name: "Roles", value:"reactionRole"},
{name: "Voting", value:"voting"},
{name: "Remove", value:"remove"}
))
.addStringOption(option => option.setName("message").setDescription("The Message ID to assign reactions to").setRequired(true))
.addStringOption(option => option.setName("emojis").setDescription("Emojis to react with seperated by ','"))
.addStringOption(option => option.setName("values").setDescription("Values assigned to emojis in order seperated by ','"));
return data.toJSON();
},
/**
*
* @param {Client} bot
* @param {MessageInteraction} interaction
*/
async run(bot, interaction) {
var message = interaction.options.getString("message")
var type = interaction.options.getString("type")
let messages = await interaction.channel.messages.fetch(message);
//console.log(messages)
if (type === "remove") {
// delete all database entries for this message
messages.reactions.removeAll()
interaction.reply("cleard message")
}
if (type === "voting" || type === "reactionRole") {
var emojis = interaction.options.getString("emojis").split(",")
var values = interaction.options.getString("values").split(",")
console.log(emojis)
console.log(values)
console.log(emojis.length === values.length)
emojis.forEach(e => {
console.log(e)
messages.react(e)
});
interaction.reply("reactions set")
}
},
}

View File

@ -1,5 +1,6 @@
const {MessageReaction, PartialMessageReaction, User} = require("discord.js") const {MessageReaction, PartialMessageReaction, User, RoleSelectMenuBuilder} = require("discord.js")
const {Connection} = require("mysql") const {Connection} = require("mysql")
const Roles = require("./Roles");
/** /**
reaction programming syntax reaction programming syntax
@ -26,8 +27,12 @@ const DevData = {
reactions: { reactions: {
"👀":{ "👀":{
Action: "role", Action: "role",
Role: "test", Role: {
Restriction: "AddOnly" Name: "Gold Role",
ID: "1051891644007469096"
},
Restriction: "AddOnly",
MessageType: "public",
} }
} }
}, },
@ -37,12 +42,14 @@ const DevData = {
"✅":{ "✅":{
Action: "vote-yes", Action: "vote-yes",
Role: "null", Role: "null",
Restriction: "null" Restriction: "null",
MessageType: "public",
}, },
"🚫":{ "🚫":{
Action: "vote-no", Action: "vote-no",
Role: "null", Role: "null",
Restriction: "null" Restriction: "null",
MessageType: "public",
} }
} }
} }
@ -55,29 +62,69 @@ const DevData = {
* @param {Connection} Database current databes instance * @param {Connection} Database current databes instance
* @param {string} Action string of current action happening * @param {string} Action string of current action happening
*/ */
const HandleReaction = async (reaction, user, database, Action) => { const HandleReaction = async (reaction, user, database, Action, bot) => {
// Handle if reaction is partial and therefor might throw api errors // Handle if reaction is partial and therefor might throw api errors
if (reaction.partial){ try{
try{ // global values
// global values let message = reaction.message
let message = reaction.message console.log(Action);
// Route Actions // Route Actions
switch (Action) { switch (Action) {
case "add": case "add":
console.log(DevData[message.id].type) // console.log(DevData[message.id].type)
break; // console.log(DevData[message.id].reactions[reaction.emoji.name].Role)
case "remove": switch (DevData[message.id].type) {
// Reverses all actions case 'reactionRole':
// at this point of development pointless to spend development time on userRoles = reaction.message.guild.members.cache.get(user.id)
break; // Detect if user already has that Role
} if (!userRoles.roles.cache.has(DevData[message.id].reactions[reaction.emoji.name].Role.ID)) {
}catch (e) { Roles.AddRole(userRoles, DevData[message.id].reactions[reaction.emoji.name].Role.Name, bot, "added via automation", message).then(() => {
console.log(e) message.channel.send(`@${user.username} got ${DevData[message.id].reactions[reaction.emoji.name].Role.Name} added`). then(async msg => {
return setTimeout(() => {
msg.delete();
},5000)
}).catch(err => {
console.log("could not send message due to error in reactions.HandleReaction: " + err)
});
}).catch(err => {
console.log("could not add role to client due to error in reactions.HandleReaction: " + err)
});
}
break;
}
break;
case "remove":
console.log("remove trigger")
// Reverses all actions
// at this point of development pointless to spend development time on
switch (DevData[message.id].type) {
case 'reactionRole':
console.log("reaction role trigger")
userRoles = reaction.message.guild.members.cache.get(user.id)
console.log(userRoles.roles.cache.has(DevData[message.id].reactions[reaction.emoji.name].Role.ID))
// Detect if user already has that Role
if (userRoles.roles.cache.has(DevData[message.id].reactions[reaction.emoji.name].Role.ID)) {
console.log("call removerole function")
Roles.RemoveRole(userRoles, DevData[message.id].reactions[reaction.emoji.name].Role.Name, bot, "removed via automation", message);
message.channel.send(`@${user.username} got ${DevData[message.id].reactions[reaction.emoji.name].Role.Name} removed`). then(async msg => {
setTimeout(() => {
msg.delete();
},5000)
}).catch(err => {
console.log("could not send message due to error in reactions.HandleReaction: " + err)
});
}
break;
}
break;
} }
} else { }catch (e) {
console.error("error while checking if reaction was partial") console.log(e)
return
} }
} }

View File

@ -4,25 +4,27 @@ const discord = require("discord.js")
@param {discord.GuildMember} userVal the target user @param {discord.GuildMember} userVal the target user
@param {string} roleName name of role to assign a user @param {string} roleName name of role to assign a user
@param {discord.Client} bot the bots instance @param {discord.Client} bot the bots instance
@param {discord.Message} message the bots instance
*/ */
const AddRole = async (userVal, roleName, bot) => { const AddRole = async (userVal, roleName, bot, reason, message) => {
// error handling // error handling
if (userVal == undefined) { throw "error user was undefined" } if (userVal == undefined) { throw "error user was undefined" }
if (roleName == undefined) { throw "error roleNames was undefined" } if (roleName == undefined) { throw "error roleNames was undefined" }
if (bot == undefined) { throw "error bot was undefined" } if (bot == undefined) { throw "error bot was undefined" }
if (typeof(userVal) != "Discord.GuildMember") { throw "error userVal is not of type Discord.GuildMember" } if (typeof(userVal) != "object") { throw "error userVal is not of type Discord.GuildMember" }
if (typeof(roleName) != "string") { throw "error roleNames is not of type string" } if (typeof(roleName) != "string") { throw "error roleNames is not of type string" }
if (typeof(bot) != "Discord.Client") { throw "error roleNames is not of type Discord.Client" } if (typeof(bot) != "object") { throw "error roleNames is not of type Discord.Client" }
// fetching required data // fetching required data
const guild = bot.guilds.cache.first() const role = bot.guilds.cache.find(guild => guild.id === message.guild.id).roles.cache.find(role => role.name === roleName)
const role = guild.roles.find("name", roleName)
if (role == undefined) { if (role == undefined) {
throw `error couldnt find role ${role}` throw `error couldnt find role ${role}`
} }
userVal.roles.add(role, reason)
userVal.roles.add(role, reason).catch(error => {
console.log("an error occured in roles.AddRole: " + err)
})
} }
/** /**
@ -35,21 +37,23 @@ const AddRoles = async (userVal, roles, bot, reason) => {
// error handling // error handling
if (userVal == undefined) { throw "error user was undefined" } if (userVal == undefined) { throw "error user was undefined" }
if (roleNames == undefined) { throw "error roleNames was undefined" } if (roleName == undefined) { throw "error roleNames was undefined" }
if (bot == undefined) { throw "error bot was undefined" } if (bot == undefined) { throw "error bot was undefined" }
if (typeof(userVal) != "Discord.GuildMember") { throw "error userVal is not of type Discord.GuildMember" } if (typeof(userVal) != "object") { throw "error userVal is not of type Discord.GuildMember" }
if (typeof(roleNames) != "object") { throw "error roleNames is not of type object" } if (typeof(roleName) != "string") { throw "error roleNames is not of type string" }
if (typeof(bot) != "Discord.Client") { throw "error roleNames is not of type Discord.Client" } if (typeof(bot) != "object") { throw "error roleNames is not of type Discord.Client" }
// fetching required data // fetching required data
const guild = bot.guilds.cache.first() const guild = bot.guilds.cache.first()
roles.forEach(r => { roles.forEach(r => {
const role = guild.roles.find("name", r.name) const role = bot.guilds.cache.find(guild => guild.id === message.guild.id).roles.cache.find(role => role.name === roleName)
if (role == undefined) { if (role == undefined) {
throw `error couldnt find role ${role}` throw `error couldnt find role ${role}`
} }
userVal.roles.add(role, r.reason)
userVal.roles.add(role, r.reason).catch(err => {console.log("error in roles.Addrole" + err)})
}) })
} }
@ -59,24 +63,27 @@ revokes a given role to a given user
@param {string} roleName name of role to assign a user @param {string} roleName name of role to assign a user
@param {discord.Client} bot the bots instance @param {discord.Client} bot the bots instance
*/ */
const RemoveRole = async (userVal, roleName, bot, reason) => { const RemoveRole = async (userVal, roleName, bot, reason, message) => {
console.log("RemoveRole Function Call")
// error handling // error handling
if (userVal == undefined) { throw "error user was undefined" } if (userVal == undefined) { throw "error user was undefined" }
if (roleName == undefined) { throw "error roleNames was undefined" } if (roleName == undefined) { throw "error roleNames was undefined" }
if (bot == undefined) { throw "error bot was undefined" } if (bot == undefined) { throw "error bot was undefined" }
if (typeof(userVal) != "Discord.GuildMember") { throw "error userVal is not of type Discord.GuildMember" } if (typeof(userVal) != "object") { throw "error userVal is not of type Discord.GuildMember" }
if (typeof(roleName) != "string") { throw "error roleNames is not of type string" } if (typeof(roleName) != "string") { throw "error roleNames is not of type string" }
if (typeof(bot) != "Discord.Client") { throw "error roleNames is not of type Discord.Client" } if (typeof(bot) != "object") { throw "error roleNames is not of type Discord.Client" }
// fetching required data // fetching required data
const guild = bot.guilds.cache.first() const role = bot.guilds.cache.find(guild => guild.id === message.guild.id).roles.cache.find(role => role.name === roleName)
const role = guild.roles.find("name", roleName)
if (role == undefined) { if (role == undefined) {
throw `error couldnt find role ${role}` throw `error couldnt find role ${role}`
} }
userVal.roles.remove(role, reason) userVal.roles.remove(role, reason).catch(error => {
console.log("an error occured in roles.AddRole: " + err)
})
} }
/** /**
@ -100,20 +107,26 @@ const RemoveRoles = async (userVal, roles, bot) => {
// error handling // error handling
if (userVal == undefined) { throw "error user was undefined" } if (userVal == undefined) { throw "error user was undefined" }
if (roleNames == undefined) { throw "error roleNames was undefined" } if (roleName == undefined) { throw "error roleNames was undefined" }
if (bot == undefined) { throw "error bot was undefined" } if (bot == undefined) { throw "error bot was undefined" }
if (typeof(userVal) != "Discord.GuildMember") { throw "error userVal is not of type Discord.GuildMember" } if (typeof(userVal) != "object") { throw "error userVal is not of type Discord.GuildMember" }
if (typeof(roleNames) != "object") { throw "error roleNames is not of type object" } if (typeof(roleName) != "string") { throw "error roleNames is not of type string" }
if (typeof(bot) != "Discord.Client") { throw "error roleNames is not of type Discord.Client" } if (typeof(bot) != "object") { throw "error roleNames is not of type Discord.Client" }
// fetching required data // fetching required data
const guild = bot.guilds.cache.first() const guild = bot.guilds.cache.first()
roles.forEach(r => { roles.forEach(r => {
const role = guild.roles.find("name", r.name) const role = bot.guilds.cache.find(guild => guild.id === message.guild.id).roles.cache.find(role => role.name === r.name)
if (role == undefined) { if (role == undefined) {
throw `error couldnt find role ${role}` throw `error couldnt find role ${role}`
} }
userVal.roles.remove(role, r.reason)
userVal.roles.remove(role, reason).catch(error => {
console.log("an error occured in roles.AddRole: " + err)
})
}) })
} }
module.exports = {AddRole, AddRoles, RemoveRole, RemoveRoles}

View File

@ -33,12 +33,14 @@ bot.on(Events.InteractionCreate, async interaction => {
// Handles Reactions // Handles Reactions
bot.on(Events.MessageReactionAdd, async (reaction, user) => { bot.on(Events.MessageReactionAdd, async (reaction, user) => {
HandleReaction(reaction, user, undefined, "add") if (user.bot) return;
HandleReaction(reaction, user, undefined, "add", bot)
}) })
bot.on(Events.MessageReactionRemove, async (reaction, user) => { bot.on(Events.MessageReactionRemove, async (reaction, user) => {
HandleReaction(reaction, user, server, "remove") if (user.bot) return;
HandleReaction(reaction, user, server, "remove", bot)
}) })
bot.login(process.env.TOKEN) bot.login(process.env.TOKEN)