Compare commits

...

2 commits

Author SHA1 Message Date
Louis Hollingworth 530e99a7d0
(#2) Members can now be in multiple guilds
Signed-off-by: Louis Hollingworth <louis@hollingworth.ch>
2023-06-18 14:05:21 +01:00
Louis Hollingworth e8f797d1e0
(#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>
2023-06-18 12:34:31 +01:00
8 changed files with 126 additions and 55 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" UUID NOT NULL DEFAULT gen_random_uuid(),
"name" TEXT NOT NULL,
"booster_role_id" TEXT,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"dgid" TEXT NOT NULL,
"duid" TEXT NOT NULL,
CONSTRAINT "member_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "guild_id_key" ON "guild"("id");
-- CreateIndex
CREATE UNIQUE INDEX "member_dgid_duid_key" ON "member"("dgid", "duid");

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

@ -1,5 +1,6 @@
generator client {
provider = "prisma-client-js"
previewFeatures = ["postgresqlExtensions"]
}
datasource db {
@ -12,3 +13,16 @@ model guild {
name String
reports_channel_id String?
}
model member {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
name String
booster_role_id String?
created_at DateTime @default(now())
/// Discord Guild ID
dgid String
/// Discord User ID
duid String
@@unique([dgid, duid])
}

View file

@ -4,35 +4,19 @@ import {
CommandInteraction,
GuildMember,
PermissionsBitField,
Role,
} from "discord.js";
import {
Discord,
Guard,
GuardFunction,
Slash,
SlashGroup,
SlashOption,
} from "discordx";
import { Discord, 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" })
export class AdminCmds {
@Slash({
description: "Set or get the configured channel for reports",
defaultMemberPermissions: PermissionsBitField.Flags.Administrator,
})
async reports(
@SlashOption({
description: "Set where the reports should be sent",
@ -92,4 +76,61 @@ 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: {
dgid_duid: {
duid: member.id,
dgid: member.guild.id,
},
},
});
if (mem) {
await prisma.member.update({
where: {
id: mem.id,
},
data: {
booster_role_id: role.id,
name: member.displayName,
},
});
} else {
await prisma.member.create({
data: {
duid: member.id,
dgid: interaction.guildId!,
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 type { Guild, Interaction, Message } from "discord.js";
import type { Interaction } from "discord.js";
import { IntentsBitField } from "discord.js";
import * as dotenv from "dotenv";
import { Client } from "discordx";
@ -23,16 +23,11 @@ export const bot = new Client({
// Debug logs are disabled in silent mode
silent: false,
// Configuration for @SimpleCommand
simpleCommand: {
prefix: "!",
},
});
bot.once("ready", async () => {
// Make sure all guilds are cached
// await bot.guilds.fetch();
await bot.guilds.fetch();
// Synchronize applications commands with Discord
await bot.initApplicationCommands();
@ -52,19 +47,6 @@ bot.on("interactionCreate", (interaction: 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() {
// The following syntax should be used in the commonjs environment
//

View file

@ -10,7 +10,7 @@
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"noImplicitAny": false,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},