From c46c1f6c0180e52f1a951b974480fd3cb4006e35 Mon Sep 17 00:00:00 2001 From: suhas Date: Tue, 14 Mar 2023 20:44:03 -0500 Subject: [PATCH] daily --- prisma/schema.prisma | 24 ++++++++++++++------- src/commands/daily.ts | 49 +++++++++++++++++++++++++++++++++++++++++++ src/index.ts | 18 +++++++++++++++- 3 files changed, 82 insertions(+), 9 deletions(-) create mode 100644 src/commands/daily.ts diff --git a/prisma/schema.prisma b/prisma/schema.prisma index c7f2f09..8cc5eef 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -11,14 +11,22 @@ datasource db { } 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 - users User[] + id String @id + welcome_message String @default("Hello %USER%!") + welcome_channel String? + welcome_on Boolean @default(false) + users User[] + dailymessages DailyMessage[] +} + +model DailyMessage { + id Int @id @default(autoincrement()) + text String + guild GuildConfig @relation(fields: [guild_id], references: [id]) + guild_id String + hour Int + minute Int + channel String } model User { diff --git a/src/commands/daily.ts b/src/commands/daily.ts new file mode 100644 index 0000000..5d6631e --- /dev/null +++ b/src/commands/daily.ts @@ -0,0 +1,49 @@ +import { PrismaClient } from '@prisma/client' +import { CommandContext, CommandOptionType, SlashCommand, SlashCreator } from 'slash-create' + +export default class DailyCommand extends SlashCommand { + constructor(creator: SlashCreator) { + super(creator, { + name: 'daily', + description: 'send daily message', + options: [ + { + name: 'hour', + description: 'the hour to send the daily message IN UTC!!', + type: CommandOptionType.INTEGER, + min_value: 0, + max_value: 24, + required: true + }, + { + name: 'minute', + description: 'the minute to send the daily message IN UTC!!', + type: CommandOptionType.INTEGER, + min_value: 0, + max_value: 59, + required: true + }, + { + name: 'message', + description: 'the message to send', + type: CommandOptionType.STRING, + required: true + } + ] + }) + } + + async run(ctx: CommandContext) { + const p = new PrismaClient() + await p.dailyMessage.create({ + data: { + hour: ctx.options.hour, + minute: ctx.options.minute, + text: ctx.options.message, + channel: ctx.channelID, + guild_id: ctx.guildID + } + }) + return `done!` + } +} diff --git a/src/index.ts b/src/index.ts index 4a679f1..71a8a97 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,7 @@ import dotenv from 'dotenv' import { SlashCreator, GatewayServer } from 'slash-create' import path from 'path' import CatLoggr from 'cat-loggr/ts' -import { Client, GatewayDispatchEvents, Events, Guild, Message, GuildMember, TextChannel } from 'discord.js' +import { Client, GatewayDispatchEvents, Events, Guild, Message, GuildMember, TextChannel, MessageMentions } from 'discord.js' import { PrismaClient } from '@prisma/client' const prisma = new PrismaClient() @@ -89,4 +89,20 @@ client.on(Events.GuildMemberAdd, async (m: GuildMember) => { } }) +setInterval(async () => { + const now = new Date() + const messages = await prisma.dailyMessage.findMany({ + where: { + hour: now.getUTCHours(), + minute: now.getUTCMinutes() + } + }) + for (const m of messages) { + const g = client.guilds.cache.get(m.guild_id) + if (!g) continue + const ch = (await g.channels.fetch(m.channel)) as TextChannel + await ch.send(m.text) + } +}, 1000 * 60) + client.login(process.env.TOKEN)