(#2) Booster roles can now be linked to members.

Next steps:
 * Remove booster roles from the DB and Guild when someone stops
   boosting
 * Add booster roles to the DB and Guild when someone starts boosting

Signed-off-by: Louis Hollingworth <louis@hollingworth.ch>
This commit is contained in:
Louis Hollingworth 2023-06-18 12:34:31 +01:00
parent 6e1e54da1b
commit e8f797d1e0
Signed by: lucxjo
GPG key ID: A11415CB3DC7809B
7 changed files with 111 additions and 45 deletions

View file

@ -0,0 +1,26 @@
-- CreateTable
CREATE TABLE "guild" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"reports_channel_id" TEXT,
CONSTRAINT "guild_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "member" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"booster_role_id" TEXT NOT NULL,
CONSTRAINT "member_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "guild_id_key" ON "guild"("id");
-- CreateIndex
CREATE UNIQUE INDEX "member_id_key" ON "member"("id");
-- CreateIndex
CREATE UNIQUE INDEX "member_booster_role_id_key" ON "member"("booster_role_id");

View file

@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "postgresql"

View file

@ -12,3 +12,9 @@ model guild {
name String name String
reports_channel_id String? reports_channel_id String?
} }
model member {
id String @id @unique
name String
booster_role_id String @unique
}

View file

@ -4,6 +4,7 @@ import {
CommandInteraction, CommandInteraction,
GuildMember, GuildMember,
PermissionsBitField, PermissionsBitField,
Role,
} from "discord.js"; } from "discord.js";
import { import {
Discord, Discord,
@ -15,24 +16,14 @@ import {
} from "discordx"; } from "discordx";
import { prisma } from "../main.js"; 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() @Discord()
@SlashGroup({ name: "admin", description: "Admin commands" }) @SlashGroup({ name: "admin", description: "Admin commands" })
@SlashGroup("admin") @SlashGroup("admin")
@Guard(AdminOnly) export class AdminCmds {
class AdminCmds { @Slash({
@Slash({ description: "Set or get the configured channel for reports" }) description: "Set or get the configured channel for reports",
defaultMemberPermissions: PermissionsBitField.Flags.Administrator,
})
async reports( async reports(
@SlashOption({ @SlashOption({
description: "Set where the reports should be sent", description: "Set where the reports should be sent",
@ -92,4 +83,57 @@ class AdminCmds {
}); });
} }
} }
@Slash({
description: "Link a user to their booster role",
name: "link_role",
defaultMemberPermissions: PermissionsBitField.Flags.ManageRoles,
})
async link_role(
@SlashOption({
description: "The user account to link",
name: "member",
required: true,
type: ApplicationCommandOptionType.User,
})
member: GuildMember,
@SlashOption({
description: "The role to link",
name: "role",
required: true,
type: ApplicationCommandOptionType.Role,
})
role: Role,
interaction: CommandInteraction
) {
let mem = await prisma.member.findUnique({
where: {
id: member.id,
},
});
if (mem) {
await prisma.member.update({
where: {
id: member.id,
},
data: {
name: mem.name != member.displayName ? member.displayName : undefined,
booster_role_id: role.id,
},
});
} else {
await prisma.member.create({
data: {
id: member.id,
name: member.displayName,
booster_role_id: role.id,
},
});
}
interaction.reply({
content: "Member booster role updated!",
ephemeral: true,
});
}
} }

View file

@ -1,10 +0,0 @@
import type { ArgsOf, Client } from "discordx";
import { Discord, On } from "discordx";
@Discord()
export class Example {
@On()
messageDelete([message]: ArgsOf<"messageDelete">, client: Client): void {
console.log("Message Deleted", client.user?.username, message.content);
}
}

15
src/events/guild_join.ts Normal file
View file

@ -0,0 +1,15 @@
import { ArgsOf, Discord, On } from "discordx";
import { prisma } from "../main.js";
@Discord()
export class GuildJoin {
@On({ event: "guildCreate" })
fn([guild]: ArgsOf<"guildCreate">) {
prisma.guild.create({
data: {
id: guild.id,
name: guild.name,
},
});
}
}

View file

@ -1,5 +1,5 @@
import { dirname, importx } from "@discordx/importer"; import { dirname, importx } from "@discordx/importer";
import type { Guild, Interaction, Message } from "discord.js"; import type { Interaction } from "discord.js";
import { IntentsBitField } from "discord.js"; import { IntentsBitField } from "discord.js";
import * as dotenv from "dotenv"; import * as dotenv from "dotenv";
import { Client } from "discordx"; import { Client } from "discordx";
@ -23,16 +23,11 @@ export const bot = new Client({
// Debug logs are disabled in silent mode // Debug logs are disabled in silent mode
silent: false, silent: false,
// Configuration for @SimpleCommand
simpleCommand: {
prefix: "!",
},
}); });
bot.once("ready", async () => { bot.once("ready", async () => {
// Make sure all guilds are cached // Make sure all guilds are cached
// await bot.guilds.fetch(); await bot.guilds.fetch();
// Synchronize applications commands with Discord // Synchronize applications commands with Discord
await bot.initApplicationCommands(); await bot.initApplicationCommands();
@ -52,19 +47,6 @@ bot.on("interactionCreate", (interaction: Interaction) => {
bot.executeInteraction(interaction); bot.executeInteraction(interaction);
}); });
bot.on("messageCreate", (message: Message) => {
bot.executeCommand(message);
});
bot.on("guildCreate", (guild: Guild) => {
prisma.guild.create({
data: {
id: guild.id,
name: guild.name,
},
});
});
async function run() { async function run() {
// The following syntax should be used in the commonjs environment // The following syntax should be used in the commonjs environment
// //