Base structure #2

This commit is contained in:
2022-11-24 02:12:52 +01:00
parent 261627570d
commit d8365128fc
20 changed files with 1528 additions and 41 deletions

7
src/commands/ci.ts Normal file
View File

@ -0,0 +1,7 @@
import * as notification from "./notification";
const array = [notification.builder.toJSON()];
export {
array
}

View File

@ -0,0 +1,39 @@
import { ChatInputCommandInteraction, Collection, Events, SlashCommandBuilder } from "discord.js";
import * as notification from "./notification";
import client from "../client";
import getDefaultEmbed from "../tools/defaultEmbeds";
const commands = new Collection<string, { builder: SlashCommandBuilder, execute: (interaction: ChatInputCommandInteraction) => Promise<void> }>();
commands.set(notification.builder.name, notification);
client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isChatInputCommand()) return;
const command = commands.get(interaction.commandName);
if (!command) {
console.error(`No command matching ${interaction.commandName} was found`);
return;
}
try {
await command.execute(interaction);
} catch (e) {
if (e instanceof Error && "stack" in e) {
console.error(e.stack);
} else {
console.error(e)
}
const embed = getDefaultEmbed();
embed.setTitle("Something went wrong");
embed.setDescription("An unexpected error occurred while processing your command. Please contact support if the problem persists");
embed.setColor(0xD01B15);
await interaction.reply({
embeds: [embed],
ephemeral: true
}).catch();
}
});

View File

@ -1,5 +1,7 @@
import { SlashCommandBuilder, PermissionFlagsBits, ChannelType, ChatInputCommandInteraction, NewsChannel, TextBasedChannel, CategoryChannel, StageChannel, TextChannel, PrivateThreadChannel, PublicThreadChannel, VoiceChannel, APIInteractionDataResolvedChannel, ForumChannel } from "discord.js";
import { getSuccessEmbed } from "../tools/defaultEmbeds";
import { SlashCommandBuilder, PermissionFlagsBits, ChannelType, ChatInputCommandInteraction, NewsChannel, TextBasedChannel, CategoryChannel, StageChannel, TextChannel, PrivateThreadChannel, PublicThreadChannel, VoiceChannel, APIInteractionDataResolvedChannel, ForumChannel, GuildBasedChannel } from "discord.js";
import getDefaultEmbed, { getSuccessEmbed } from "../tools/defaultEmbeds";
import { database, GuildSetting } from "../data";
import client from "../client";
const builder = new SlashCommandBuilder();
builder.setName("logchannel");
@ -8,37 +10,119 @@ builder.setDMPermission(false);
builder.setDefaultMemberPermissions(PermissionFlagsBits.ManageChannels);
builder.addChannelOption((option) => {
option.addChannelTypes(ChannelType.GuildText, ChannelType.GuildAnnouncement);
option.setName("Channel");
option.setName("channel");
option.setDescription("The channel to send notifications to");
option.setRequired(true);
option.setRequired(false);
return option;
});
const execute = (interaction: ChatInputCommandInteraction) => {
const channel:TextBasedChannel = getTextBasedChannel(interaction.options.getChannel("Channel", true));
//TODO Save channel in config
const embed = getSuccessEmbed();
embed.setDescription(`Log channel has been set to <#${channel.id}>`);
interaction.reply({
embeds: [embed],
ephemeral: true
async function getGuildSetting(guildID: string): Promise<GuildSetting> {
let guildSetting = await database.getRepository(GuildSetting).findOne({
where: {
id: guildID
}
});
const logMessage = getSuccessEmbed();
logMessage.setDescription("This channel has been selected as the log channel");
logMessage.addFields({
if (!guildSetting) {
guildSetting = new GuildSetting();
guildSetting.id = guildID;
guildSetting.isPremiumUntil = null;
guildSetting.notificationChannelID = null;
}
return guildSetting;
}
async function getGuildChannel(guildID: string, channelID: string): Promise<GuildBasedChannel | null> {
const guild = await client.guilds.fetch(guildID);
if (!guild) return null;
const channel = await guild.channels.fetch(channelID);
return channel;
}
async function resetNotificationChannel(guildSetting: GuildSetting, interaction: ChatInputCommandInteraction): Promise<void> {
const logChannel = guildSetting.notificationChannelID ? await getGuildChannel(guildSetting.id, guildSetting.notificationChannelID) : null;
guildSetting.notificationChannelID = null;
await database.getRepository(GuildSetting).save(guildSetting);
const logEmbed = getDefaultEmbed();
logEmbed.setTitle("Settings changed");
logEmbed.setDescription("Log channel has been disabled");
logEmbed.addFields({
name: "This action was performed by",
value: `${interaction.user.tag} (${interaction.user.id})`
});
channel.send({
embeds: [logMessage]
if (logChannel && logChannel.isTextBased()) {
logChannel.send({
embeds: [logEmbed]
}).catch();
}
const embed = getSuccessEmbed();
embed.setDescription("Log channel has been disabled");
interaction.reply({
embeds: [embed],
ephemeral: true
}).catch();
}
const execute = async (interaction: ChatInputCommandInteraction) => {
if (!interaction.guildId) throw new Error("Command can only be used inside a guild");
const optionVal = interaction.options.getChannel("channel", false);
const guildSetting = await getGuildSetting(interaction.guildId);
if (!optionVal) return await resetNotificationChannel(guildSetting, interaction);
const channel = getTextBasedChannel(optionVal);
if (guildSetting.notificationChannelID) {
const oldLogChannel = await getGuildChannel(guildSetting.id, guildSetting.notificationChannelID);
const embed = getDefaultEmbed();
embed.setTitle("Settings changed");
embed.setDescription(`Log channel has been switched to <#${channel.id}>`);
embed.addFields({
name: "This action was performed by",
value: `${interaction.user.tag} (${interaction.user.id})`
});
if (oldLogChannel && oldLogChannel.isTextBased()) {
oldLogChannel.send({
embeds: [embed]
}).catch();
}
}
guildSetting.notificationChannelID = channel.id;
await database.getRepository(GuildSetting).save(guildSetting);
const embed = getDefaultEmbed();
embed.setTitle("Settings changed");
embed.setDescription("This channel has been set as the log channel");
embed.addFields({
name: "This action was performed by",
value: `${interaction.user.tag} (${interaction.user.id})`
});
channel.send({
embeds: [ embed ]
}).catch();
const reply = getSuccessEmbed();
reply.setDescription(`Log channel was set to <#${channel.id}>`);
interaction.reply({
embeds: [ reply ],
ephemeral: true
}).catch();
return;
}
function getTextBasedChannel(channel: CategoryChannel | NewsChannel | StageChannel | TextChannel | PrivateThreadChannel | PublicThreadChannel<boolean> | VoiceChannel | ForumChannel | APIInteractionDataResolvedChannel): TextBasedChannel {
if (channel.type === ChannelType.DM || channel.type === ChannelType.GroupDM || channel.type === ChannelType.GuildAnnouncement || channel.type === ChannelType.GuildText || channel.type === ChannelType.PublicThread || channel.type === ChannelType.PrivateThread || channel.type === ChannelType.GuildVoice) {
if (channel.type === ChannelType.GuildAnnouncement || channel.type === ChannelType.GuildText || channel.type === ChannelType.PublicThread || channel.type === ChannelType.PrivateThread || channel.type === ChannelType.GuildVoice) {
return channel as TextBasedChannel;
}