Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 137 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
},
"dependencies": {
"@discord-nestjs/common": "^5.1.4",
"@discord-nestjs/core": "^5.2.1",
"@discord-nestjs/core": "^5.3.0",
"@discordjs/builders": "^1.4.0",
"@discordjs/rest": "^1.5.0",
"@liaoliaots/nestjs-redis": "^9.0.5",
"@nestjs/common": "^9.3.8",
"@nestjs/config": "^2.3.1",
Expand All @@ -52,6 +54,7 @@
"helmet": "^6.0.1",
"ioredis": "^5.3.1",
"is-ci": "^3.0.1",
"pg": "^8.9.0",
"prisma": "^4.10.1",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Warnings:

- A unique constraint covering the columns `[name]` on the table `DiscordBotMessages` will be added. If there are existing duplicate values, this will fail.

*/
-- CreateIndex
CREATE UNIQUE INDEX "DiscordBotMessages_name_key" ON "DiscordBotMessages"("name");
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- CreateTable
CREATE TABLE "StreamDate" (
"id" SERIAL NOT NULL,
"streamer" TEXT NOT NULL,
"streamHour" BIGINT NOT NULL,

CONSTRAINT "StreamDate_pkey" PRIMARY KEY ("id")
);
11 changes: 11 additions & 0 deletions prisma/migrations/20230217120030_maj_dtb/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Warnings:

- A unique constraint covering the columns `[streamer]` on the table `StreamDate` will be added. If there are existing duplicate values, this will fail.

*/
-- AlterTable
ALTER TABLE "StreamDate" ALTER COLUMN "streamHour" DROP NOT NULL;

-- CreateIndex
CREATE UNIQUE INDEX "StreamDate_streamer_key" ON "StreamDate"("streamer");
8 changes: 7 additions & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ model Donation {
model DiscordBotMessages {
id Int @id @default(autoincrement())
createdAt BigInt
name String
name String @unique
message String
}

model StreamDate {
id Int @id @default(autoincrement())
streamer String @unique
streamHour BigInt?
}
31 changes: 31 additions & 0 deletions src/discord/RemindStream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Injectable } from "@nestjs/common"
import { Cron } from "@nestjs/schedule"
import { Client } from "discord.js"
import { PrismaService } from "../prisma.service"

@Injectable()
export class RemindStream {
private readonly client: Client
private readonly prismaService: PrismaService

@Cron("0 1 * * * *") // dtb check every hours
async sendMessage() {
const date = new Date()
const timestamp = Math.floor(date.getTime() / 1000)
const remind = timestamp + 7200

// remove 2 hour to the current date

const data = await this.prismaService.StreamDate.findMany({
where: { streamHour: remind }
})

for (const item of data) {
const user = await this.client.users.fetch(item.streamer)

if (user === data.streamer) {
user.send("attention tu passe en live a " + data.streamHour + ".") // Replace with your message
}
}
}
}
63 changes: 63 additions & 0 deletions src/discord/add-stream.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Injectable } from "@nestjs/common"
import { PrismaService } from "../prisma.service"
import { CommandInteraction } from "discord.js"
import { SlashCommandPipe } from "@discord-nestjs/common"
import { Handler, InteractionEvent } from "@discord-nestjs/core"
import { AddStreamDto } from "./dto/add-stream.dto"

@Injectable()
export class AddStreamDate {
constructor(private readonly prismaService: PrismaService) {}
@Handler()
async updateMessage(
@InteractionEvent(SlashCommandPipe) payload: AddStreamDto,
interaction: CommandInteraction
): Promise<void> {
// await interaction.reply("ok")
const dateString = payload.date
const dateRegex = dateString.split(/[\s/]+/) // Split the string into date and time parts
const [day, month, year, hours, minutes] = dateRegex // Destructure the date and time parts

const timestampStream = new Date(`${year}-${month}-${day}T${hours}:${minutes}:00`).getTime() / 1000 // Create a new Date object and get its timestamp (in seconds)

try {
await this.prismaService.StreamDate.upsert({
where: {
name: payload.member
},
create: {
streamer: payload.member,
streamHour: timestampStream
},
update: {
streamHour: timestampStream
}
})

await interaction.reply("Message updated")
} catch (error: any) {
await interaction.reply("Error updating message")
}
}

// constructor(private readonly prismaService: PrismaService) {}
// async create(member: GuildMember) {
// await this.prismaService.StreamDate.create({
// data: {
// streamer: member.user.username
// }
// })
// }
// async addAllMembers(client: Client) {
// const guild = client.guilds.cache.get("1074690667235790869")
// if (guild) {
// const members = await guild.members.fetch()
// for (const [, member] of members) {
// if (member) {
// await this.create(member)
// }
// }
// console.log(`Added ${members.size} members to the database.`)
// }
// }
}
12 changes: 10 additions & 2 deletions src/discord/bot.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ export class BotGateway {
async onGuildMemberAdd(member: GuildMember) {
this.logger.log(`New member ${member.user.tag} joined!`)

// Send a private message to the new member
await member.send("Welcome to the server!")
// Load the bienvenu message from the database
const message = await this.prismaService.discordBotMessages.findFirst({
where: { name: "WELCOME" }
})
// Send a set data Base private message to the new member
if (message) {
return await member.send(message.message)
}
// Send a default private message to the new member
await member.send("Bienvenue sur le serveur!")
}
}
14 changes: 12 additions & 2 deletions src/discord/discord.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { ExtendedConfigService } from "../config/config.service"
import { GatewayIntentBits } from "discord.js"
import { BotGateway } from "./bot.gateway"
import { DatabaseModule } from "../database/database.module"
import { UpdateMessage } from "./update-message.command"
import { AddStreamDate } from "./add-stream.command"
import { RemindStream } from "./RemindStream"
//import { AddStream } from "./add-stream.command"

@Module({
imports: [
Expand All @@ -20,11 +24,17 @@ import { DatabaseModule } from "../database/database.module"
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.GuildMembers
]
}
},
registerCommandOptions: [
{
forGuild: "1074690667235790869",
removeCommandsBefore: true
}
]
})
}),
DatabaseModule
],
providers: [BotGateway]
providers: [BotGateway, UpdateMessage, AddStreamDate, RemindStream]
})
export class DiscordModule {}
Loading