generated from lucxjo/template
(#1) User reporting now complete
closes #1 Signed-off-by: Louis Hollingworth <louis@hollingworth.ch>
This commit is contained in:
parent
faca0b2fda
commit
6770bd8d35
7 changed files with 856 additions and 349 deletions
75
src/commands/admin.ts
Normal file
75
src/commands/admin.ts
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
import { ApplicationCommandOptionType, Channel, CommandInteraction, GuildMember, PermissionsBitField } from "discord.js";
|
||||
import { Discord, Guard, GuardFunction, Slash, SlashGroup, SlashOption } from "discordx";
|
||||
import { prisma } from "../main.js";
|
||||
|
||||
const AdminOnly: GuardFunction<CommandInteraction> = async (
|
||||
arg,
|
||||
client,
|
||||
next
|
||||
) => {
|
||||
const argObj = arg instanceof Array ? arg[0] : arg;
|
||||
const user = argObj.user as GuildMember
|
||||
|
||||
if (user.permissions.has(PermissionsBitField.Flags.Administrator)) await next();
|
||||
}
|
||||
|
||||
@Discord()
|
||||
@SlashGroup({ name: "admin", description: "Admin commands" })
|
||||
@SlashGroup("admin")
|
||||
@Guard(AdminOnly)
|
||||
class AdminCmds {
|
||||
@Slash({description: "Set or get the configured channel for reports"})
|
||||
async reports(
|
||||
@SlashOption({
|
||||
description: "Set where the reports should be sent",
|
||||
name: "reports_channel",
|
||||
required: false,
|
||||
type: ApplicationCommandOptionType.Channel
|
||||
})
|
||||
channel: Channel,
|
||||
interaction: CommandInteraction
|
||||
) {
|
||||
if (channel) {
|
||||
await prisma.guild.findUnique({
|
||||
where: {
|
||||
id: interaction.guildId!
|
||||
}
|
||||
}).then(async (data) => {
|
||||
if (data) {
|
||||
await prisma.guild.update({
|
||||
where: {
|
||||
id: data.id
|
||||
},
|
||||
data: {
|
||||
reports_channel_id: channel.id,
|
||||
}
|
||||
})
|
||||
} else {
|
||||
await prisma.guild.create({
|
||||
data: {
|
||||
id: interaction.guildId!,
|
||||
name: interaction.guild!.name,
|
||||
reports_channel_id: channel.id
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
interaction.reply(`<#${channel.id}> is now setup to receive reports.`)
|
||||
})
|
||||
} else {
|
||||
await prisma.guild.findUnique({
|
||||
where: {
|
||||
id: interaction.guildId!
|
||||
}
|
||||
}).then((data) => {
|
||||
if (data) {
|
||||
interaction.reply(`<#${data.reports_channel_id}> is currently receiving reports for this guild.`)
|
||||
} else {
|
||||
interaction.reply("Reports are currently disabled for this guild")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
26
src/main.ts
26
src/main.ts
|
|
@ -61,9 +61,9 @@ bot.on("guildCreate", (guild: Guild) => {
|
|||
data: {
|
||||
id: guild.id,
|
||||
name: guild.name,
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
async function run() {
|
||||
// The following syntax should be used in the commonjs environment
|
||||
|
|
@ -71,7 +71,9 @@ async function run() {
|
|||
// await importx(__dirname + "/{events,commands}/**/*.{ts,js}");
|
||||
|
||||
// The following syntax should be used in the ECMAScript environment
|
||||
await importx(`${dirname(import.meta.url)}/{events,commands,menus}/**/*.{ts,js}`);
|
||||
await importx(
|
||||
`${dirname(import.meta.url)}/{events,commands,menus}/**/*.{ts,js}`
|
||||
);
|
||||
|
||||
// Let's start the bot
|
||||
if (!process.env.DISCORD_TOKEN) {
|
||||
|
|
@ -82,10 +84,12 @@ async function run() {
|
|||
await bot.login(process.env.DISCORD_TOKEN);
|
||||
}
|
||||
|
||||
run().then(async () => {
|
||||
await prisma.$disconnect();
|
||||
}).catch(async (e) => {
|
||||
console.error(e);
|
||||
await prisma.$disconnect();
|
||||
process.exit(1);
|
||||
});
|
||||
run()
|
||||
.then(async () => {
|
||||
await prisma.$disconnect();
|
||||
})
|
||||
.catch(async (e) => {
|
||||
console.error(e);
|
||||
await prisma.$disconnect();
|
||||
process.exit(1);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,76 +1,103 @@
|
|||
import { ActionRowBuilder, ApplicationCommandType, Client, MessageContextMenuCommandInteraction, ModalBuilder, TextChannel, TextInputBuilder, TextInputStyle } from "discord.js";
|
||||
import { ContextMenu, Discord } from "discordx";
|
||||
import {
|
||||
ActionRowBuilder,
|
||||
ApplicationCommandType,
|
||||
Client,
|
||||
MessageContextMenuCommandInteraction,
|
||||
ModalBuilder,
|
||||
ModalSubmitInteraction,
|
||||
TextChannel,
|
||||
TextInputBuilder,
|
||||
TextInputStyle,
|
||||
} from "discord.js";
|
||||
import { ContextMenu, Discord, ModalComponent } from "discordx";
|
||||
import { prisma } from "../main.js";
|
||||
|
||||
@Discord()
|
||||
export class Report {
|
||||
@ContextMenu({
|
||||
name: "Report user",
|
||||
type: ApplicationCommandType.User,
|
||||
})
|
||||
async reportUser(interaction: MessageContextMenuCommandInteraction, client: Client) {
|
||||
@ContextMenu({
|
||||
name: "Report user",
|
||||
type: ApplicationCommandType.User,
|
||||
})
|
||||
async reportUser(
|
||||
interaction: MessageContextMenuCommandInteraction,
|
||||
client: Client
|
||||
) {
|
||||
if (interaction.guildId) {
|
||||
let data = await prisma.guild.findUnique({
|
||||
where: {
|
||||
id: interaction.guildId!,
|
||||
},
|
||||
});
|
||||
|
||||
if (interaction.guildId) {
|
||||
let data = await prisma.guild.findUnique({
|
||||
where: {
|
||||
id: interaction.guildId!
|
||||
}
|
||||
})
|
||||
if (!data) {
|
||||
await prisma.guild.create({
|
||||
data: {
|
||||
id: interaction.guildId!,
|
||||
name: interaction.guild!.name,
|
||||
},
|
||||
});
|
||||
|
||||
if (!data) {
|
||||
await prisma.guild.create({
|
||||
data: {
|
||||
id: interaction.guildId!,
|
||||
name: interaction.guild!.name
|
||||
}
|
||||
})
|
||||
|
||||
data = await prisma.guild.findUnique({
|
||||
where: {
|
||||
id: interaction.guildId!
|
||||
}
|
||||
})
|
||||
}
|
||||
data = await prisma.guild.findUnique({
|
||||
where: {
|
||||
id: interaction.guildId!,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (!data!.reports_channel_id) {
|
||||
interaction.reply(
|
||||
"It looks like your guild hasn't set up this feature, please speak to the staff."
|
||||
);
|
||||
} else {
|
||||
const modal = new ModalBuilder()
|
||||
.setTitle("Report user")
|
||||
.setCustomId("report-user");
|
||||
.setTitle("Report user")
|
||||
.setCustomId("report_user");
|
||||
|
||||
const userId = new TextInputBuilder()
|
||||
.setCustomId("user-id")
|
||||
.setValue(interaction.targetId)
|
||||
.setLabel("User ID")
|
||||
.setStyle(TextInputStyle.Short);
|
||||
.setCustomId("user-id")
|
||||
.setValue(interaction.targetId)
|
||||
.setLabel("User ID")
|
||||
.setStyle(TextInputStyle.Short);
|
||||
|
||||
const reason = new TextInputBuilder()
|
||||
.setCustomId("reason")
|
||||
.setLabel("Reason")
|
||||
.setStyle(TextInputStyle.Paragraph);
|
||||
.setCustomId("reason")
|
||||
.setLabel("Reason")
|
||||
.setStyle(TextInputStyle.Paragraph);
|
||||
|
||||
modal.addComponents(
|
||||
new ActionRowBuilder<TextInputBuilder>().addComponents(userId),
|
||||
new ActionRowBuilder<TextInputBuilder>().addComponents(reason),
|
||||
new ActionRowBuilder<TextInputBuilder>().addComponents(userId),
|
||||
new ActionRowBuilder<TextInputBuilder>().addComponents(reason)
|
||||
);
|
||||
|
||||
interaction.showModal(modal).then(() => {
|
||||
interaction.editReply({
|
||||
content: "Reported user.",
|
||||
});
|
||||
|
||||
// client.channels.fetch("channel-id").then((channel) => {
|
||||
// if (channel?.isTextBased()) {
|
||||
// let ct = channel as TextChannel;
|
||||
// ct.send({
|
||||
// content: `User <@${interaction.user.id}> reported <@${interaction.targetId}> for: ${reason.data.value ?? ""}`,
|
||||
// });
|
||||
// }
|
||||
//})
|
||||
})
|
||||
} else {
|
||||
interaction.reply({
|
||||
content: "It looks like you aren't in a guild, you can only report within guilds"
|
||||
})
|
||||
}
|
||||
interaction.showModal(modal)
|
||||
}
|
||||
} else {
|
||||
interaction.reply({
|
||||
content:
|
||||
"It looks like you aren't in a guild, you can only report within guilds",
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ModalComponent()
|
||||
async report_user(interaction: ModalSubmitInteraction, client: Client) {
|
||||
const [user_id, reason] = ['user-id', 'reason'].map((id) => interaction.fields.getTextInputValue(id))
|
||||
const data = await prisma.guild.findUnique({
|
||||
where: {
|
||||
id: interaction.guildId!
|
||||
}
|
||||
})
|
||||
client.channels.fetch(data!.reports_channel_id!).then((channel) => {
|
||||
if (channel?.isTextBased()) {
|
||||
let ct = channel as TextChannel;
|
||||
ct.send({
|
||||
content: `<@${interaction.user.id}> reported <@${
|
||||
user_id
|
||||
}> for: ${reason}`,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
interaction.reply({content: "Reported user.", ephemeral: true})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue