Files
Yumi/src/discord/Say.ts
T

136 lines
4.7 KiB
TypeScript
Raw Normal View History

2022-06-29 15:27:42 +07:00
import { Message, Permissions, Interaction, CommandInteraction, PermissionsBitField } from 'discord.js';
2022-06-07 03:08:27 +07:00
import { I18n } from 'i18n';
2022-03-03 20:56:49 +07:00
2022-06-06 14:59:53 +07:00
import DiscordModule, { HybridInteractionMessage } from '../utils/DiscordModule';
import {
makeSuccessEmbed,
sendHybridInteractionMessageResponse,
makeErrorEmbed,
makeInfoEmbed
} from '../utils/DiscordMessage';
2022-06-07 03:08:27 +07:00
import Locale from '../services/Locale';
2021-09-15 04:10:42 -07:00
2021-09-19 04:02:50 -07:00
const EMBEDS = {
2022-06-07 03:08:27 +07:00
SAY_INFO: (data: HybridInteractionMessage, locale: I18n) => {
2022-06-06 14:59:53 +07:00
return makeInfoEmbed({
2022-06-07 03:08:27 +07:00
title: locale.__('say.title'),
description: locale.__('say.info'),
2021-09-19 04:02:50 -07:00
fields: [
{
2022-06-07 03:08:27 +07:00
name: locale.__('common.available_args'),
value: locale.__('say.valid_args')
2021-09-19 04:02:50 -07:00
}
],
2022-06-07 03:08:27 +07:00
user: data.getUser()
2021-09-19 04:02:50 -07:00
});
},
2022-06-07 03:08:27 +07:00
NO_PERMISSION: (data: HybridInteractionMessage, locale: I18n) => {
2022-06-06 14:59:53 +07:00
return makeErrorEmbed({
2022-06-07 03:08:27 +07:00
title: locale.__('common.no_permissions'),
description: locale.__(
2022-06-27 20:28:01 +07:00
'common.no_permissions_description', {
PERMISSIONS: 'VIEW_CHANNEL, SEND_MESSAGES, MANAGE_CHANNELS'
}
2022-06-07 03:08:27 +07:00
),
user: data.getUser()
2021-09-19 04:02:50 -07:00
});
},
2022-06-07 03:08:27 +07:00
SAY_ERROR: (data: HybridInteractionMessage, locale: I18n, error: Error) => {
return makeErrorEmbed({
2022-06-07 03:08:27 +07:00
title: locale.__('say.error'),
description: error.message ? error.message : undefined,
user: data.getUser()
});
},
2022-06-07 03:08:27 +07:00
SUCCESSFULLY_SAID: (data: HybridInteractionMessage, locale: I18n) => {
2022-06-06 14:59:53 +07:00
return makeSuccessEmbed({
2022-06-07 03:08:27 +07:00
title: locale.__('say.success'),
user: data.getUser()
2021-09-19 04:02:50 -07:00
});
}
2022-06-06 14:59:53 +07:00
};
2021-09-19 04:02:50 -07:00
2022-03-03 20:56:49 +07:00
export default class Say extends DiscordModule {
2022-06-06 14:59:53 +07:00
public id = 'Discord_Say';
public commands = ['say'];
public commandInteractionName = 'say';
2022-03-03 20:56:49 +07:00
async GuildOnModuleCommand(args: any, message: Message) {
await this.run(new HybridInteractionMessage(message), args);
2021-09-17 05:21:01 -07:00
}
2022-06-06 14:59:53 +07:00
async GuildModuleCommandInteractionCreate(interaction: CommandInteraction) {
2022-03-03 20:56:49 +07:00
await this.run(new HybridInteractionMessage(interaction), interaction.options);
2021-09-17 05:21:01 -07:00
}
2022-03-03 20:56:49 +07:00
async run(data: HybridInteractionMessage, args: any) {
2022-06-07 03:08:27 +07:00
const guild = data.getGuild();
if (!guild) return;
const locale = await Locale.getGuildLocale(guild.id);
2021-09-19 04:02:50 -07:00
let query;
2021-09-15 04:10:42 -07:00
2022-06-06 14:59:53 +07:00
if (data.isMessage()) {
2022-03-03 20:56:49 +07:00
const message = data.getMessage();
2022-06-06 14:59:53 +07:00
if (!message.member) return;
2022-03-03 20:56:49 +07:00
2022-06-06 14:59:53 +07:00
if (
!message.member.permissions.has([
2022-06-29 15:27:42 +07:00
PermissionsBitField.Flags.ViewChannel,
PermissionsBitField.Flags.SendMessages,
PermissionsBitField.Flags.ManageChannels,
2022-06-06 14:59:53 +07:00
])
)
return await sendHybridInteractionMessageResponse(data, {
2022-06-07 03:08:27 +07:00
embeds: [EMBEDS.NO_PERMISSION(data, locale)]
2022-06-06 14:59:53 +07:00
});
2022-03-03 20:56:49 +07:00
2022-06-06 14:59:53 +07:00
if (args.length === 0)
return await sendHybridInteractionMessageResponse(data, {
2022-06-07 03:08:27 +07:00
embeds: [EMBEDS.SAY_INFO(data, locale)]
2022-06-06 14:59:53 +07:00
});
2021-09-17 05:21:01 -07:00
2022-06-06 14:59:53 +07:00
query = args.join(' ');
2022-06-29 15:27:42 +07:00
} else if (data.isApplicationCommand()) {
2022-03-03 20:56:49 +07:00
const interaction = data.getSlashCommand();
2022-06-06 14:59:53 +07:00
if (
!data
.getGuild()!
.members.cache.get(interaction.user.id)
2022-06-29 15:27:42 +07:00
?.permissions.has([PermissionsBitField.Flags.Administrator])
2022-06-06 14:59:53 +07:00
)
return await sendHybridInteractionMessageResponse(data, {
2022-06-07 03:08:27 +07:00
embeds: [EMBEDS.NO_PERMISSION(data, locale)]
2022-06-06 14:59:53 +07:00
});
2021-09-19 04:02:50 -07:00
2022-06-29 15:27:42 +07:00
query = interaction.options.get('message', true).value?.toString();
2021-09-19 04:02:50 -07:00
}
2021-09-15 04:10:42 -07:00
2022-06-29 15:27:42 +07:00
if (data.isApplicationCommand() && data.getChannel()) {
try {
await data.getChannel()!.send({ content: query });
await data
.getMessageComponentInteraction()
2022-06-07 03:08:27 +07:00
.reply({ ephemeral: true, embeds: [EMBEDS.SUCCESSFULLY_SAID(data, locale)] });
} catch (err: any) {
return await sendHybridInteractionMessageResponse(data, {
2022-06-07 03:08:27 +07:00
embeds: [EMBEDS.SAY_ERROR(data, locale, err)]
});
}
2022-06-06 14:59:53 +07:00
} else if (data.isMessage()) {
2022-03-03 20:56:49 +07:00
const message = data.getMessage();
2022-06-06 14:59:53 +07:00
if (message.deletable) await message.delete();
2021-09-19 04:02:50 -07:00
try {
await data.getChannel()!.send({ content: query });
} catch (err: any) {
return await sendHybridInteractionMessageResponse(data, {
2022-06-07 03:08:27 +07:00
embeds: [EMBEDS.SAY_ERROR(data, locale, err)]
});
}
2021-09-17 05:21:01 -07:00
}
2021-09-15 04:10:42 -07:00
}
2022-06-06 14:59:53 +07:00
}