initial commit
This commit is contained in:
90
commands/roles/autorole.ts
Normal file
90
commands/roles/autorole.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
import discord, {
|
||||
CommandInteraction,
|
||||
CommandInteractionOptionResolver,
|
||||
SlashCommandBuilder,
|
||||
SlashCommandRoleOption,
|
||||
SlashCommandStringOption,
|
||||
InteractionResponse,
|
||||
} from 'discord.js';
|
||||
import {HydratedDocument} from 'mongoose';
|
||||
|
||||
import {Command} from '../../classes/command';
|
||||
import {logger} from '../../logger';
|
||||
import {AutoroleMsg, autoroleMsgModel} from '../../models/AutoroleMsg';
|
||||
import {Guild, guildModel} from '../../models/Guild';
|
||||
|
||||
type CIOR = CommandInteractionOptionResolver;
|
||||
|
||||
class Autorole extends Command{
|
||||
get name(){return "autorole";}
|
||||
get description(){return "Setup a autorole message.";}
|
||||
async execute(interaction: CommandInteraction): Promise<void>{
|
||||
try{
|
||||
let role = (interaction.options as CIOR).getRole('role');
|
||||
if(!role)
|
||||
throw Error('role not exist');
|
||||
const title: string | null =
|
||||
(interaction.options as CIOR).getString('title');
|
||||
if(!title)
|
||||
throw Error('title not exist');
|
||||
const message: string | null =
|
||||
(interaction.options as CIOR).getString('message');
|
||||
if(!message)
|
||||
throw Error('message not exist');
|
||||
|
||||
if(!interaction.guild || !interaction.guild.id || !interaction.guild.name)
|
||||
throw Error('guildId not exist');
|
||||
if(!interaction.channelId)
|
||||
throw Error('channelId not exist');
|
||||
let content: string = `>> **Auto Role: ${title}** <<\n`;
|
||||
content += '*React to give yourself a role.*\n\n';
|
||||
content += `${message}`;
|
||||
let msg: discord.Message =
|
||||
await (await interaction.reply({content: content})).fetch();
|
||||
const guild: HydratedDocument<Guild> | null =
|
||||
await guildModel.findOneAndUpdate(
|
||||
{id: interaction.guild.id},
|
||||
{$set: {name: interaction.guild.name}},
|
||||
{upsert: true, new: true}
|
||||
);
|
||||
if(!guild)
|
||||
throw Error('guild not exist');
|
||||
const autoroleMsg: HydratedDocument<AutoroleMsg> | null =
|
||||
await autoroleMsgModel.findOneAndUpdate(
|
||||
{guild: guild, messageId: msg.id, roleId: role.id},
|
||||
{}, {upsert: true, new: true}
|
||||
);
|
||||
if(!autoroleMsg)
|
||||
throw Error('autoroleMsg not exist');
|
||||
}catch(err: unknown){
|
||||
let message;
|
||||
if(err instanceof Error) message = err.message;
|
||||
else message = String(message);
|
||||
logger.error(`While executing "/autorole", ${message}`);
|
||||
await interaction.reply({content: `While executing "/autorole", ${message}`});
|
||||
}
|
||||
}
|
||||
override build(): SlashCommandBuilder |
|
||||
Omit<SlashCommandBuilder, "addSubcommand" | "addSubcommandGroup">{
|
||||
return new SlashCommandBuilder()
|
||||
.setName(this.name)
|
||||
.setDescription(this.description)
|
||||
.addRoleOption((option: SlashCommandRoleOption) => option
|
||||
.setName('role')
|
||||
.setDescription('The role that autorole gives when reacted.')
|
||||
.setRequired(true)
|
||||
)
|
||||
.addStringOption((option: SlashCommandStringOption) => option
|
||||
.setName('title')
|
||||
.setDescription('The title displayed with the autorole message.')
|
||||
.setRequired(true)
|
||||
)
|
||||
.addStringOption((option: SlashCommandStringOption) => option
|
||||
.setName('message')
|
||||
.setDescription('The additional message displayed with the autorole message.')
|
||||
.setRequired(true)
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
export const command = new Autorole();
|
||||
Reference in New Issue
Block a user