diff --git a/src/commands/settings/logs/attachment.ts b/src/commands/settings/logs/attachment.ts new file mode 100644 index 0000000..0842923 --- /dev/null +++ b/src/commands/settings/logs/attachment.ts @@ -0,0 +1,157 @@ +import { ChannelType } from 'discord-api-types'; +import Discord, { CommandInteraction, MessageActionRow, MessageButton } from "discord.js"; +import EmojiEmbed from "../../../utils/generateEmojiEmbed.js"; +import confirmationMessage from "../../../utils/confirmationMessage.js"; +import getEmojiByName from "../../../utils/getEmojiByName.js"; +import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; +import { WrappedCheck } from "jshaiku"; +import client from "../../../utils/client.js"; + +const command = (builder: SlashCommandSubcommandBuilder) => + builder + .setName("attachments") + .setDescription("Where attachments should be logged to (Premium only)") + .addChannelOption(option => option.setName("channel").setDescription("The channel to log attachments in").addChannelTypes([ + ChannelType.GuildNews, ChannelType.GuildText + ]).setRequired(false)) + +const callback = async (interaction: CommandInteraction): Promise => { + let m; + m = await interaction.reply({embeds: [new EmojiEmbed() + .setTitle("Loading") + .setStatus("Danger") + .setEmoji("NUCLEUS.LOADING") + ], ephemeral: true, fetchReply: true}); + if (interaction.options.getChannel("channel")) { + let channel + try { + channel = interaction.options.getChannel("channel") + } catch { + return await interaction.editReply({embeds: [new EmojiEmbed() + .setEmoji("CHANNEL.TEXT.DELETE") + .setTitle("Attachment Log Channel") + .setDescription("The channel you provided is not a valid channel") + .setStatus("Danger") + ]}) + } + channel = channel as Discord.TextChannel + if (channel.guild.id != interaction.guild.id) { + return interaction.editReply({embeds: [new EmojiEmbed() + .setTitle("Attachment Log Channel") + .setDescription(`You must choose a channel in this server`) + .setStatus("Danger") + .setEmoji("CHANNEL.TEXT.DELETE") + ]}); + } + let confirmation = await new confirmationMessage(interaction) + .setEmoji("CHANNEL.TEXT.EDIT") + .setTitle("Attachment Log Channel") + .setDescription( + `This will be the channel all attachments will be sent to.\n\n` + + `Are you sure you want to set the attachment log channel to <#${channel.id}>?` + ) + .setColor("Warning") + .setInverted(true) + .send(true) + if (confirmation.cancelled) return + if (confirmation.success) { + try { + await client.database.guilds.write(interaction.guild.id, {"logging.attachments.channel": channel.id}) + const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger + try { + let data = { + meta:{ + type: 'attachmentChannelUpdate', + displayName: 'Attachment Log Channel Updated', + calculateType: 'nucleusSettingsUpdated', + color: NucleusColors.yellow, + emoji: "CHANNEL.TEXT.EDIT", + timestamp: new Date().getTime() + }, + list: { + memberId: entry(interaction.user.id, `\`${interaction.user.id}\``), + changedBy: entry(interaction.user.id, renderUser(interaction.user)), + channel: entry(channel.id, renderChannel(channel)), + }, + hidden: { + guild: interaction.guild.id + } + } + log(data); + } catch {} + } catch (e) { + return interaction.editReply({embeds: [new EmojiEmbed() + .setTitle("Attachment Log Channel") + .setDescription(`Something went wrong and the attachment log channel could not be set`) + .setStatus("Danger") + .setEmoji("CHANNEL.TEXT.DELETE") + ], components: []}); + } + } else { + return interaction.editReply({embeds: [new EmojiEmbed() + .setTitle("Attachment Log Channel") + .setDescription(`No changes were made`) + .setStatus("Success") + .setEmoji("CHANNEL.TEXT.CREATE") + ], components: []}); + } + } + let clicks = 0; + let data = await client.database.guilds.read(interaction.guild.id); + let channel = data.logging.staff.channel; + while (true) { + await interaction.editReply({embeds: [new EmojiEmbed() + .setTitle("Attachment Log Channel") + .setDescription( + channel ? `Your attachment log channel is currently set to <#${channel}>` : "This server does not have an attachment log channel" + + (client.database.premium.hasPremium(interaction.guild.id) ? "" : "\n\nThis server does not have premium, so this feature is disabled") + ) + .setStatus("Success") + .setEmoji("CHANNEL.TEXT.CREATE") + ], components: [new MessageActionRow().addComponents([new MessageButton() + .setCustomId("clear") + .setLabel(clicks ? "Click again to confirm" : "Reset channel") + .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id")) + .setStyle("DANGER") + .setDisabled(!channel) + ])]}); + let i; + try { + i = await m.awaitMessageComponent({time: 300000}); + } catch(e) { break } + i.deferUpdate() + if (i.component.customId == "clear") { + clicks += 1; + if (clicks == 2) { + clicks = 0; + await client.database.guilds.write(interaction.guild.id, {}, ["logging.announcements.channel"]) + channel = undefined; + } + } else { + break + } + } + await interaction.editReply({embeds: [new EmojiEmbed() + .setTitle("Attachment Log Channel") + .setDescription(channel ? `Your attachment log channel is currently set to <#${channel}>` : "This server does not have an attachment log channel") + .setStatus("Success") + .setEmoji("CHANNEL.TEXT.CREATE") + .setFooter({text: "Message closed"}) + ], components: [new MessageActionRow().addComponents([new MessageButton() + .setCustomId("clear") + .setLabel("Clear") + .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) + .setStyle("SECONDARY") + .setDisabled(true) + ])]}); +} + +const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { + let member = (interaction.member as Discord.GuildMember) + if (!member.permissions.has("MANAGE_GUILD")) throw "You must have the Manage Server permission to use this command" + return true; +} + +export { command }; +export { callback }; +export { check }; diff --git a/src/commands/settings/logs/staff.ts b/src/commands/settings/logs/staff.ts new file mode 100644 index 0000000..3cb7230 --- /dev/null +++ b/src/commands/settings/logs/staff.ts @@ -0,0 +1,154 @@ +import { ChannelType } from 'discord-api-types'; +import Discord, { CommandInteraction, MessageActionRow, MessageButton } from "discord.js"; +import EmojiEmbed from "../../../utils/generateEmojiEmbed.js"; +import confirmationMessage from "../../../utils/confirmationMessage.js"; +import getEmojiByName from "../../../utils/getEmojiByName.js"; +import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; +import { WrappedCheck } from "jshaiku"; +import client from "../../../utils/client.js"; + +const command = (builder: SlashCommandSubcommandBuilder) => + builder + .setName("staff") + .setDescription("Settings for the staff notifications channel") + .addChannelOption(option => option.setName("channel").setDescription("The channel to set the staff notifications channel to").addChannelTypes([ + ChannelType.GuildNews, ChannelType.GuildText + ]).setRequired(false)) + +const callback = async (interaction: CommandInteraction): Promise => { + let m; + m = await interaction.reply({embeds: [new EmojiEmbed() + .setTitle("Loading") + .setStatus("Danger") + .setEmoji("NUCLEUS.LOADING") + ], ephemeral: true, fetchReply: true}); + if (interaction.options.getChannel("channel")) { + let channel + try { + channel = interaction.options.getChannel("channel") + } catch { + return await interaction.editReply({embeds: [new EmojiEmbed() + .setEmoji("CHANNEL.TEXT.DELETE") + .setTitle("Staff Notifications Channel") + .setDescription("The channel you provided is not a valid channel") + .setStatus("Danger") + ]}) + } + channel = channel as Discord.TextChannel + if (channel.guild.id != interaction.guild.id) { + return interaction.editReply({embeds: [new EmojiEmbed() + .setTitle("Staff Notifications Channel") + .setDescription(`You must choose a channel in this server`) + .setStatus("Danger") + .setEmoji("CHANNEL.TEXT.DELETE") + ]}); + } + let confirmation = await new confirmationMessage(interaction) + .setEmoji("CHANNEL.TEXT.EDIT") + .setTitle("Staff Notifications Channel") + .setDescription( + `This will be the channel all notifications, updates, user reports etc. will be sent to.\n\n` + + `Are you sure you want to set the staff notifications channel to <#${channel.id}>?` + ) + .setColor("Warning") + .setInverted(true) + .send(true) + if (confirmation.cancelled) return + if (confirmation.success) { + try { + await client.database.guilds.write(interaction.guild.id, {"logging.staff.channel": channel.id}) + const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger + try { + let data = { + meta:{ + type: 'staffChannelUpdate', + displayName: 'Staff Notifications Channel Updated', + calculateType: 'nucleusSettingsUpdated', + color: NucleusColors.yellow, + emoji: "CHANNEL.TEXT.EDIT", + timestamp: new Date().getTime() + }, + list: { + memberId: entry(interaction.user.id, `\`${interaction.user.id}\``), + changedBy: entry(interaction.user.id, renderUser(interaction.user)), + channel: entry(channel.id, renderChannel(channel)), + }, + hidden: { + guild: interaction.guild.id + } + } + log(data); + } catch {} + } catch (e) { + return interaction.editReply({embeds: [new EmojiEmbed() + .setTitle("Staff Notifications Channel") + .setDescription(`Something went wrong and the staff notifications channel could not be set`) + .setStatus("Danger") + .setEmoji("CHANNEL.TEXT.DELETE") + ], components: []}); + } + } else { + return interaction.editReply({embeds: [new EmojiEmbed() + .setTitle("Staff Notifications Channel") + .setDescription(`No changes were made`) + .setStatus("Success") + .setEmoji("CHANNEL.TEXT.CREATE") + ], components: []}); + } + } + let clicks = 0; + let data = await client.database.guilds.read(interaction.guild.id); + let channel = data.logging.staff.channel; + while (true) { + await interaction.editReply({embeds: [new EmojiEmbed() + .setTitle("Staff Notifications channel") + .setDescription(channel ? `Your staff notifications channel is currently set to <#${channel}>` : "This server does not have a staff notifications channel") + .setStatus("Success") + .setEmoji("CHANNEL.TEXT.CREATE") + ], components: [new MessageActionRow().addComponents([new MessageButton() + .setCustomId("clear") + .setLabel(clicks ? "Click again to confirm" : "Reset channel") + .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id")) + .setStyle("DANGER") + .setDisabled(!channel) + ])]}); + let i; + try { + i = await m.awaitMessageComponent({time: 300000}); + } catch(e) { break } + i.deferUpdate() + if (i.component.customId == "clear") { + clicks += 1; + if (clicks == 2) { + clicks = 0; + await client.database.guilds.write(interaction.guild.id, {}, ["logging.staff.channel"]) + channel = undefined; + } + } else { + break + } + } + await interaction.editReply({embeds: [new EmojiEmbed() + .setTitle("Staff Notifications channel") + .setDescription(channel ? `Your staff notifications channel is currently set to <#${channel}>` : "This server does not have a staff notifications channel") + .setStatus("Success") + .setEmoji("CHANNEL.TEXT.CREATE") + .setFooter({text: "Message closed"}) + ], components: [new MessageActionRow().addComponents([new MessageButton() + .setCustomId("clear") + .setLabel("Clear") + .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) + .setStyle("SECONDARY") + .setDisabled(true) + ])]}); +} + +const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { + let member = (interaction.member as Discord.GuildMember) + if (!member.permissions.has("MANAGE_GUILD")) throw "You must have the Manage Server permission to use this command" + return true; +} + +export { command }; +export { callback }; +export { check }; diff --git a/src/commands/settings/stats/_meta.ts b/src/commands/settings/stats/_meta.ts new file mode 100644 index 0000000..7443a9e --- /dev/null +++ b/src/commands/settings/stats/_meta.ts @@ -0,0 +1,4 @@ +const name = "stats"; +const description = "Settings for stats channels"; + +export { name, description }; \ No newline at end of file diff --git a/src/commands/settings/stats/remove.ts b/src/commands/settings/stats/remove.ts new file mode 100644 index 0000000..8e3a64a --- /dev/null +++ b/src/commands/settings/stats/remove.ts @@ -0,0 +1,120 @@ +import { ChannelType } from 'discord-api-types'; +import Discord, { CommandInteraction, MessageActionRow, MessageButton } from "discord.js"; +import EmojiEmbed from "../../../utils/generateEmojiEmbed.js"; +import confirmationMessage from "../../../utils/confirmationMessage.js"; +import getEmojiByName from "../../../utils/getEmojiByName.js"; +import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; +import { WrappedCheck } from "jshaiku"; +import client from "../../../utils/client.js"; + +const command = (builder: SlashCommandSubcommandBuilder) => + builder + .setName("remove") + .setDescription("Stops updating channels when a member joins or leaves") + .addChannelOption(option => option.setName("channel").setDescription("The channel to stop updating").addChannelTypes([ + ChannelType.GuildNews, ChannelType.GuildText + ]).setRequired(true)) + +const callback = async (interaction: CommandInteraction): Promise => { + let m; + m = await interaction.reply({embeds: [new EmojiEmbed() + .setTitle("Loading") + .setStatus("Danger") + .setEmoji("NUCLEUS.LOADING") + ], ephemeral: true, fetchReply: true}); + if (interaction.options.getChannel("channel")) { + let config = client.database.guilds.read(interaction.guild.id); + let channel + try { + channel = interaction.options.getChannel("channel") + } catch { + return await interaction.editReply({embeds: [new EmojiEmbed() + .setEmoji("CHANNEL.TEXT.DELETE") + .setTitle("Stats Channel") + .setDescription("The channel you provided is not a valid channel") + .setStatus("Danger") + ]}) + } + channel = channel as Discord.TextChannel + if (channel.guild.id != interaction.guild.id) { + return interaction.editReply({embeds: [new EmojiEmbed() + .setTitle("Stats Channel") + .setDescription(`You must choose a channel in this server`) + .setStatus("Danger") + .setEmoji("CHANNEL.TEXT.DELETE") + ]}); + } + // check if the channel is not in the list + let allow = false; + for (let c of config.stats) { if (c.channel == channel.id) allow = true; } + if (!allow) { + return interaction.editReply({embeds: [new EmojiEmbed() + .setTitle("Stats Channel") + .setDescription(`That channel is not a stats channel`) + .setStatus("Danger") + .setEmoji("CHANNEL.TEXT.DELETE") + ]}); + } + let confirmation = await new confirmationMessage(interaction) + .setEmoji("CHANNEL.TEXT.EDIT") + .setTitle("Stats Channel") + .setDescription(`Are you sure you want to stop <#${channel.id}> updating?`) + .setColor("Warning") + .setInverted(true) + .send(true) + if (confirmation.cancelled) return + if (confirmation.success) { + try { + let channel = interaction.options.getChannel("channel") + await client.database.guilds.write(interaction.guild.id, {}, [`stats.${channel.id}`]); + const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger + try { + let data = { + meta:{ + type: 'statsChannelUpdate', + displayName: 'Stats Channel Removed', + calculateType: 'nucleusSettingsUpdated', + color: NucleusColors.red, + emoji: "CHANNEL.TEXT.EDIT", + timestamp: new Date().getTime() + }, + list: { + memberId: entry(interaction.user.id, `\`${interaction.user.id}\``), + changedBy: entry(interaction.user.id, renderUser(interaction.user)), + channel: entry(channel.id, renderChannel(channel)), + }, + hidden: { + guild: interaction.guild.id + } + } + log(data); + } catch {} + } catch (e) { + console.log(e) + return interaction.editReply({embeds: [new EmojiEmbed() + .setTitle("Stats Channel") + .setDescription(`Something went wrong and the stats channel could not be reset`) + .setStatus("Danger") + .setEmoji("CHANNEL.TEXT.DELETE") + ], components: []}); + } + } else { + return interaction.editReply({embeds: [new EmojiEmbed() + .setTitle("Stats Channel") + .setDescription(`No changes were made`) + .setStatus("Success") + .setEmoji("CHANNEL.TEXT.CREATE") + ], components: []}); + } + } +} + +const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { + let member = (interaction.member as Discord.GuildMember) + if (!member.permissions.has("MANAGE_GUILD")) throw "You must have the Manage Server permission to use this command" + return true; +} + +export { command }; +export { callback }; +export { check }; diff --git a/src/commands/settings/stats/set.ts b/src/commands/settings/stats/set.ts new file mode 100644 index 0000000..10c3011 --- /dev/null +++ b/src/commands/settings/stats/set.ts @@ -0,0 +1,126 @@ +import { ChannelType } from 'discord-api-types'; +import Discord, { CommandInteraction, MessageActionRow, MessageButton } from "discord.js"; +import EmojiEmbed from "../../../utils/generateEmojiEmbed.js"; +import confirmationMessage from "../../../utils/confirmationMessage.js"; +import getEmojiByName from "../../../utils/getEmojiByName.js"; +import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; +import { WrappedCheck } from "jshaiku"; +import client from "../../../utils/client.js"; +import convertCurlyBracketString from '../../../utils/convertCurlyBracketString.js'; +import {callback as statsChannelAddCallback} from "../../../reflex/statsChannelUpdate.js"; + +const command = (builder: SlashCommandSubcommandBuilder) => + builder + .setName("set") + .setDescription("Adds or edits a channel which will update when members join or leave") + .addChannelOption(option => option.setName("channel").setDescription("The channel to modify").addChannelTypes([ + ChannelType.GuildNews, ChannelType.GuildText + ]).setRequired(true)) + .addStringOption(option => option.setName("name").setDescription("The channel name").setRequired(true).setAutocomplete(true)) + +const callback = async (interaction: CommandInteraction): Promise => { + console.log(interaction.options.getString("name")) + let m; + m = await interaction.reply({embeds: [new EmojiEmbed() + .setTitle("Loading") + .setStatus("Danger") + .setEmoji("NUCLEUS.LOADING") + ], ephemeral: true, fetchReply: true}); + if (interaction.options.getChannel("channel")) { + let config = client.database.guilds.read(interaction.guild.id); + let channel + try { + channel = interaction.options.getChannel("channel") + } catch { + return await interaction.editReply({embeds: [new EmojiEmbed() + .setEmoji("CHANNEL.TEXT.DELETE") + .setTitle("Stats Channel") + .setDescription("The channel you provided is not a valid channel") + .setStatus("Danger") + ]}) + } + channel = channel as Discord.TextChannel + if (channel.guild.id != interaction.guild.id) { + return interaction.editReply({embeds: [new EmojiEmbed() + .setTitle("Stats Channel") + .setDescription(`You must choose a channel in this server`) + .setStatus("Danger") + .setEmoji("CHANNEL.TEXT.DELETE") + ]}); + } + let newName = await convertCurlyBracketString(interaction.options.getString("name"), null, null, interaction.guild.name, interaction.guild.members) + if (interaction.options.getChannel("channel").type === "GUILD_TEXT") { + newName = newName.toLowerCase().replace(/[\s]/g, "-") + } + let confirmation = await new confirmationMessage(interaction) + .setEmoji("CHANNEL.TEXT.EDIT") + .setTitle("Stats Channel") + .setDescription(`Are you sure you want to set <#${channel.id}> to a stats channel?\n\n*Preview: ${newName}*`) + .setColor("Warning") + .setInverted(true) + .send(true) + if (confirmation.cancelled) return + if (confirmation.success) { + try { + let name = interaction.options.getString("name") + let channel = interaction.options.getChannel("channel") + await client.database.guilds.write(interaction.guild.id, {[`stats.${channel.id}`]: {name: name, enabled: true}}); + const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger + try { + let data = { + meta:{ + type: 'statsChannelUpdate', + displayName: 'Stats Channel Updated', + calculateType: 'nucleusSettingsUpdated', + color: NucleusColors.yellow, + emoji: "CHANNEL.TEXT.EDIT", + timestamp: new Date().getTime() + }, + list: { + memberId: entry(interaction.user.id, `\`${interaction.user.id}\``), + changedBy: entry(interaction.user.id, renderUser(interaction.user)), + channel: entry(channel.id, renderChannel(channel)), + name: entry(interaction.options.getString("name"), `\`${interaction.options.getString("name")}\``) + }, + hidden: { + guild: interaction.guild.id + } + } + log(data); + } catch {} + } catch (e) { + console.log(e) + return interaction.editReply({embeds: [new EmojiEmbed() + .setTitle("Stats Channel") + .setDescription(`Something went wrong and the stats channel could not be set`) + .setStatus("Danger") + .setEmoji("CHANNEL.TEXT.DELETE") + ], components: []}); + } + } else { + return interaction.editReply({embeds: [new EmojiEmbed() + .setTitle("Stats Channel") + .setDescription(`No changes were made`) + .setStatus("Success") + .setEmoji("CHANNEL.TEXT.CREATE") + ], components: []}); + } + await statsChannelAddCallback(client, interaction.member); + return interaction.editReply({embeds: [new EmojiEmbed() + .setTitle("Stats Channel") + .setDescription(`The stats channel has been set to <#${channel.id}>`) + .setStatus("Success") + .setEmoji("CHANNEL.TEXT.CREATE") + ], components: []}); + } +} + +const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { + let member = (interaction.member as Discord.GuildMember) + if (!member.permissions.has("MANAGE_GUILD")) throw "You must have the Manage Server permission to use this command" + return true; +} + +export { command }; +export { callback }; +export { check }; diff --git a/src/reflex/statsChannelUpdate.ts b/src/reflex/statsChannelUpdate.ts new file mode 100644 index 0000000..51d4329 --- /dev/null +++ b/src/reflex/statsChannelUpdate.ts @@ -0,0 +1,35 @@ +import convertCurlyBracketString from '../utils/convertCurlyBracketString.js' +import singleNotify from '../utils/singleNotify.js'; +import client from '../utils/client.js'; + +interface PropSchema { enabled: boolean, name: string } + +export async function callback(_, member) { + console.log("UPDATING STATS CHANNEL") + let guild = await client.guilds.fetch(member.guild.id) + let config = await client.database.guilds.read(guild.id); + Object.entries(config.getKey("stats")).forEach(async ([channel, props]) => { + if ((props as PropSchema).enabled) { + let string = (props as PropSchema).name + if (!string) return + string = await convertCurlyBracketString(string, member.id, member.displayName, guild.name, guild.members) + let fetchedChannel; + try { + fetchedChannel = await guild.channels.fetch(channel) + } catch (e) { fetchedChannel = null } + if (!fetchedChannel) { + return singleNotify( + "statsChannelDeleted", + guild.id, + "One or more of your stats channels have been deleted. Please open the settings menu to change this.", + "Critical" + ) + } + try { + await fetchedChannel.setName(string) + } catch (e) { + console.error(e) + } + } + }); +}