You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.8 KiB

const {Command, Random, ErrorMessage, ErrorType} = require('../../lib.js')
const {EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle} = require('discord.js');
const roleRuleM = require('../../models/autoRoleRule');
class setrustcommitschannel extends Command{
constructor(client){
super(client, {
name: 'setrolerule',
aliases:['rolerule','setrr'],
description: 'Sets a auto role rule. Requires four arguments in the following order: MessageID; RoleID; Emoji. \n MessageID refers to the message where Rem will look for reactions and then set a role to a user.',
needsAdmin:true,
});
this.client = client;
}
async run(message, args)
{
// Validate the command arguments
const validationResult = await this.confirmArgs(message, args);
if (validationResult === 'Error') return new ErrorMessage(this.client).send(ErrorType.Arguments, message)
// Create a new role rule object
//check if a role rule with there is already a role rule like this in this guild
// this is !roleID !roleEmoji !mID !roleEmoji
var rule = new roleRuleM();
rule.gID = message.guildId
rule.mID = validationResult.mID;
rule.roleID = validationResult.roleID;
rule.roleEmoji = validationResult.roleEmoji;
rule.roleName = validationResult.roleName;
rule.save(err=>
{
if(err)console.log(err);
})
const embed = new EmbedBuilder()
const randomID = Random();
embed.setFooter({text:'Rem-chan on ', iconURL:"https://i.imgur.com/g6FSNhL.png"})
embed.setAuthor({name:"Rem-chan", iconURL:"https://i.imgur.com/g6FSNhL.png",url:'https://rem.wordfights.com/addtodiscord'});
embed.setColor(0x110809);
embed.setTimestamp()
embed.setTitle(`${rule.roleName}'s rule defined.`)
const row = new ActionRowBuilder()
.addComponents(
new ButtonBuilder()
.setCustomId(randomID)
.setLabel('Remove this.')
.setStyle(ButtonStyle.Primary),
);
const filter = i => i.customId === randomID;
message.channel.send({embeds: [embed], components: [row] });
const collector = message.channel.createMessageComponentCollector({ filter, time: 60000 });
collector.on('collect', async m =>
{
_delete(message, m);
});
collector.on('end', async m=>
{
_delete(message,m);
})
}
/**
*
* @param {Map<String,String>} message
* @param {Array<String>} args
* @returns {RoleModel}
*
*/
async confirmArgs(message,args)
{
if(args.length<3) return 'Error'
var mID = args[0];
var roleID = args[1];
var roleEmoji = args[2].split(':')[2].split('>')[0];
var roleName;
var m = await (async ()=>
{
for(const [id, channel] of this.client.channels.cache)
{
if(channel.type == 0)
{
try {
const exists = await this.client.channels.cache.get(id).messages.fetch(mID);
if(exists) return m = true;
} catch (error) {}
}
}
})();
const r = message.guild.roles.cache.get(roleID);
const e = message.guild.emojis.cache.get(roleEmoji);
if(!m || !r || !e) return 'Error'
roleName = r.name;
return {mID, roleID, roleEmoji, roleName}
}
}module.exports = setrustcommitschannel;
function _delete(message, m)
{
try {
message.delete();
m.message.delete();
} catch (error) {
console.log(error)
}
}