From 0ea8a441db05ceda53044ac80b8b3ecd5c66bfff Mon Sep 17 00:00:00 2001 From: suhas Date: Mon, 13 Mar 2023 22:23:01 -0500 Subject: [PATCH] rps and welcomer but i stage all of them --- prisma/schema.prisma | 1 + src/commands/setwelcomemessage.ts | 29 +++++++++++++++++++++ src/commands/togglewelcomer.ts | 42 +++++++++++++++++++++++++++++++ src/index.ts | 17 +++++++++++-- 4 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 src/commands/setwelcomemessage.ts create mode 100644 src/commands/togglewelcomer.ts diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 5543c25..c7f2f09 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -14,6 +14,7 @@ model GuildConfig { id String @id welcome_message String @default("Hello %USER%!") welcome_channel String? + welcome_on Boolean @default(false) daily_message_text String @default("daily message") daily_message_channel String? daily_message_time DateTime @default(now()) @db.Timetz diff --git a/src/commands/setwelcomemessage.ts b/src/commands/setwelcomemessage.ts new file mode 100644 index 0000000..5cd36aa --- /dev/null +++ b/src/commands/setwelcomemessage.ts @@ -0,0 +1,29 @@ +import { PrismaClient } from '@prisma/client' +import { CommandContext, SlashCommand, SlashCreator, CommandOptionType } from 'slash-create' + +export default class SetWelcomeMessageCommand extends SlashCommand { + constructor(creator: SlashCreator) { + super(creator, { + name: 'setwelcomemessage', + description: 'set the welcome message, %USER% replaces to a mention of the joined user', + options: [ + { + name: 'message', + description: 'the message (leave blank to see current message)', + type: CommandOptionType.STRING + } + ] + }) + } + + async run(ctx: CommandContext) { + const prisma = new PrismaClient() + if (ctx.options.message == null) { + const g = await prisma.guildConfig.findFirst({ where: { id: ctx.guildID } }) + return `the current welcome message is ${g.welcome_message}` + } + + await prisma.guildConfig.update({ where: { id: ctx.guildID }, data: { welcome_message: ctx.options.message } }) + return `updated your welcome message to ${ctx.options.message}!` + } +} diff --git a/src/commands/togglewelcomer.ts b/src/commands/togglewelcomer.ts new file mode 100644 index 0000000..0d2fdf3 --- /dev/null +++ b/src/commands/togglewelcomer.ts @@ -0,0 +1,42 @@ +import { PrismaClient } from '@prisma/client'; +import { CommandContext, CommandOptionType, SlashCommand, SlashCreator } from 'slash-create'; + +export default class ToggleWelcomerCommand extends SlashCommand { + constructor(creator: SlashCreator) { + super(creator, { + name: 'toggle_welcomer', + description: 'toggle the welcome', + options: [ + { + name: 'setting', + type: CommandOptionType.BOOLEAN, + description: 'whether it is on or not (leave blank to see the current setting)' + } + ] + }) + } + + async run(ctx: CommandContext) { + const p = new PrismaClient() + if (ctx.options.setting == null) { + const gcf = await p.guildConfig.findFirst({ + where: { + id: ctx.guildID + } + }) + + return `the welcomer is currently ${gcf.welcome_on ? 'on' : 'off'}` + } + + await p.guildConfig.update({ + where: { + id: ctx.guildID + }, + data: { + welcome_on: ctx.options.setting + } + }) + + return `set welcomer to ${ctx.options.setting ? 'on' : 'off'}` + } +} diff --git a/src/index.ts b/src/index.ts index af906c4..daa511d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,8 @@ import dotenv from 'dotenv' -import { SlashCreator, GatewayServer } from 'slash-create' +import { SlashCreator, GatewayServer, Member } from 'slash-create' import path from 'path' import CatLoggr from 'cat-loggr/ts' -import { Client, GatewayDispatchEvents, Events, Guild, Message } from 'discord.js' +import { Client, GatewayDispatchEvents, Events, Guild, Message, GuildMember, TextChannel } from 'discord.js' import { PrismaClient } from '@prisma/client' const prisma = new PrismaClient() @@ -76,4 +76,17 @@ client.on(Events.MessageCreate, async (msg: Message) => { }) }) +client.on(Events.GuildMemberAdd, async (m: GuildMember) => { + const gc = await prisma.guildConfig.findFirst({ + where: { + id: m.guild.id + } + }) + + if (gc.welcome_on) { + const ch = (await m.guild.channels.fetch(gc.welcome_channel)) as TextChannel + await ch.send(gc.welcome_message.replace('%USER%', `<@${m.id}>`)) + } +}) + client.login(process.env.TOKEN)