Skip to content

Commit

Permalink
feat: testing channels + better delete logs + more
Browse files Browse the repository at this point in the history
  • Loading branch information
wdhdev committed Sep 30, 2023
1 parent ae2b962 commit 053d7a7
Show file tree
Hide file tree
Showing 9 changed files with 406 additions and 52 deletions.
299 changes: 299 additions & 0 deletions src/commands/bot-admin/testing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,299 @@
import Command from "../../classes/Command";
import ExtendedClient from "../../classes/ExtendedClient";
import { CommandInteraction, TextChannel } from "discord.js";

import { emojis as emoji } from "../../config";
import { randomUUID } from "crypto";

import TestingChannel from "../../models/TestingChannel";

const command: Command = {
name: "testing",
description: "Create and manage testing channels.",
options: [
{
type: 1,
name: "create",
description: "Create a testing channel."
},

{
type: 1,
name: "delete",
description: "Delete a testing channel. Can only be used in a testing channel."
},

{
type: 2,
name: "user",
description: "Add or remove users from your testing channel.",
options: [
{
type: 1,
name: "add",
description: "Add a user to your testing channel.",
options: [
{
type: 6,
name: "user",
description: "The user to add.",
required: true
}
]
},

{
type: 1,
name: "remove",
description: "Remove a user from your testing channel.",
options: [
{
type: 6,
name: "user",
description: "The user to remove.",
required: true
}
]
}
]
}
],
default_member_permissions: null,
botPermissions: ["ManageChannels"],
requiredRoles: ["botAdmin"],
cooldown: 10,
enabled: true,
deferReply: true,
ephemeral: true,
async execute(interaction: CommandInteraction & any, client: ExtendedClient, Discord: typeof import("discord.js")) {
try {
if(interaction.options.getSubcommand() === "create") {
const creating = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.setDescription(`${emoji.ping} Creating a testing channel...`)

await interaction.editReply({ embeds: [creating] });

const channel = await interaction.guild.channels.create({
name: `testing-${randomUUID().slice(0, 8)}`,
type: Discord.ChannelType.GuildText,
permissionOverwrites: [
{
id: interaction.guild.id,
deny: ["ViewChannel"],
allow: ["EmbedLinks", "ReadMessageHistory", "SendMessages", "UseExternalEmojis"]
},

{
id: client.user.id,
allow: ["ViewChannel", "ManageChannels", "ManageMessages"]
},

{
id: interaction.user.id,
allow: ["ViewChannel"]
}
]
})

const data = await new TestingChannel({
channel: channel.id,
created: Date.now(),
owner: interaction.user.id
}).save()

const welcome = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.setTitle("Your Testing Channel")
.setDescription(`Welcome to your testing channel, **${interaction.user.globalName || interaction.user.username}**!\n\nThis channel has been setup with overrides so only you and server admins can access it.\n\n**This channel will be automatically deleted after 24 hours.**`)
.addFields (
{ name: "Your Permissions", value: "```yaml\n- Embed Links\n- Read Message History\n- Send Messages\n- Use External Emojis\n- View Channel\n```" },
{ name: "Delete Channel", value: `To delete this channel, use the command: \`/testing delete\`` }
)
.setTimestamp()

const channelInfo = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.setTitle("Channel Information")
.addFields (
{ name: "Owner", value: `${interaction.user}` },
{ name: "Created", value: `<t:${data.created.toString().slice(0, -3)}:f> (<t:${data.created.toString().slice(0, -3)}:R>)` },
{ name: "Expires", value: `<t:${(data.created + 86400000).toString().slice(0, -3)}:f> (<t:${(data.created + 86400000).toString().slice(0, -3)}:R>)` }
)
.setTimestamp()

const welcomeMsg = await channel.send({ content: `${interaction.user}`, embeds: [welcome, channelInfo] });

await welcomeMsg.pin();

const created = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.setDescription(`${emoji.tick} Created testing channel: ${channel}`)

await interaction.editReply({ embeds: [created] });
return;
}

if(interaction.options.getSubcommand() === "delete") {
const channel = interaction.channel as TextChannel;

if(!channel.name.startsWith("testing-")) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} This command can only be used in a testing channel.`)

await interaction.editReply({ embeds: [error] });
return;
}

const data = await TestingChannel.findOne({ channel: channel.id });

if(interaction.user.id !== data.owner) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} You do not own this testing channel!`)

await interaction.editReply({ embeds: [error] });
return;
}

const deleting = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.setDescription(`${emoji.ping} Deleting testing channel...`)

await interaction.editReply({ embeds: [deleting] });

await channel.delete();
await data.delete();
return;
}

if(interaction.options.getSubcommandGroup() === "user") {
const user = interaction.options.getUser("user");
const channel = interaction.channel as TextChannel;

if(interaction.options.getSubcommand() === "add") {
if(!channel.name.startsWith("testing-")) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} This command can only be used in a testing channel.`)

await interaction.editReply({ embeds: [error] });
return;
}

const data = await TestingChannel.findOne({ channel: channel.id });

if(interaction.user.id !== data.owner) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} You do not own this testing channel!`)

await interaction.editReply({ embeds: [error] });
return;
}

const member = await interaction.guild.members.fetch(user.id);

if(!member) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} ${user} is not in this server!`)

await interaction.editReply({ embeds: [error] });
return;
}

const perms = channel.permissionOverwrites.cache.get(member.id);

if(perms) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} ${user} is already in this testing channel!`)

await interaction.editReply({ embeds: [error] });
return;
}

await channel.permissionOverwrites.create(member.id, { ViewChannel: true });

const welcome = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.setDescription(`${user} has been added to the testing channel.`)

interaction.channel.send({ embeds: [welcome] });

const added = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.setDescription(`${emoji.tick} Added ${member} to the testing channel.`)

await interaction.editReply({ embeds: [added] });
return;
}

if(interaction.options.getSubcommand() === "remove") {
if(!channel.name.startsWith("testing-")) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} This command can only be used in a testing channel.`)

await interaction.editReply({ embeds: [error] });
return;
}

const data = await TestingChannel.findOne({ channel: channel.id });

if(interaction.user.id !== data.owner) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} You do not own this testing channel!`)

await interaction.editReply({ embeds: [error] });
return;
}

const member = await interaction.guild.members.fetch(user.id);

if(!member) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} ${user} is not in this server!`)

await interaction.editReply({ embeds: [error] });
return;
}

const perms = channel.permissionOverwrites.cache.get(member.id);

if(!perms) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} ${user} is not in this testing channel!`)

await interaction.editReply({ embeds: [error] });
return;
}

await perms.delete();

const goodbye = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.setDescription(`${user} has been removed from the testing channel.`)

interaction.channel.send({ embeds: [goodbye] });

const removed = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.setDescription(`${emoji.tick} Removed ${member} from the testing channel.`)

await interaction.editReply({ embeds: [removed] });
return;
}
}
} catch(err) {
client.logCommandError(err, interaction, Discord);
}
}
}

export = command;
31 changes: 22 additions & 9 deletions src/commands/dev/poll-ping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,25 @@ const command: Command = {
requiredRoles: [],
cooldown: 0,
enabled: true,
deferReply: false,
ephemeral: false,
deferReply: true,
ephemeral: true,
async execute(interaction: CommandInteraction, client: ExtendedClient, Discord: typeof import("discord.js")) {
try {
// Return error if the user is not allowed to use the command
if(!client.config_main.pollPingAllowed.includes(interaction.user.id)) {
await interaction.reply({ embeds: [noPermissionCommand], ephemeral: true });
// Return error if the command is not in the primary guild
if(interaction.guild.id !== client.config_main.primaryGuild) {
const error = new Discord.EmbedBuilder()
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} This command can only be used in the primary guild.`)

await interaction.editReply({ embeds: [error] });
return;
}

// Return error if the user does not have the pollPingAllowed role
const roles = await interaction.guild.members.fetch(interaction.user.id).then(member => member.roles.cache.map(role => role.id));

if(!roles.includes(client.config_roles.pollPingAllowed)) {
await interaction.editReply({ embeds: [noPermissionCommand] });
return;
}

Expand All @@ -37,7 +49,7 @@ const command: Command = {
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} This command can only be used in <#${client.config_channels.devQuestions}>.`)

await interaction.reply({ embeds: [error], ephemeral: true });
await interaction.editReply({ embeds: [error] });
return;
}

Expand All @@ -48,7 +60,7 @@ const command: Command = {
.setColor(client.config_embeds.error)
.setDescription(`${emoji.cross} This command is on cooldown, it can be used <t:${Math.floor((client.lastPoll + 3600000) / 1000)}:R>.`)

await interaction.reply({ embeds: [error], ephemeral: true });
await interaction.editReply({ embeds: [error] });
return;
}

Expand All @@ -58,12 +70,13 @@ const command: Command = {
client.lastPoll = Date.now();

// Ping the role
await interaction.reply({ content: `<@&${client.config_roles.pollPing}>` });
await interaction.channel.send({ content: `testing` });
await interaction.deleteReply();

// Log the command
const log = new Discord.EmbedBuilder()
.setColor(client.config_embeds.default)
.setTitle("Poll Ping Command Used")
.setTitle("Poll Ping")
.addFields (
{ name: "User", value: `${interaction.user}` },
{ name: "Reason", value: reason }
Expand Down
Loading

0 comments on commit 053d7a7

Please sign in to comment.