main
suhas 2 years ago
parent 93f765b88c
commit c46c1f6c01

@ -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 {

@ -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!`
}
}

@ -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)

Loading…
Cancel
Save