Skip to content

Commit

Permalink
added ticker
Browse files Browse the repository at this point in the history
  • Loading branch information
filip326 committed Aug 27, 2023
1 parent ae7e8ee commit 62b9626
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/Bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,18 @@ async function init (): Promise<void> {
}
})

const tickers = fs.readdirSync(join(__dirname, "tick")).filter(file => file.endsWith('.js'))
for (const file of tickers) {
const ticker = (await import(`./tick/${file}`)).default
// check if ticker is a valid ticker
if (!ticker || typeof ticker !== "function") {
console.error("Ticker file is not valid " + file)
continue
}
setInterval(() => ticker(client), 1000 * 60)
console.log(`Ticker ${file} loaded`)
}

registerCommands(client, logManager.logger('Command-Registrierung'), [
...commands.map(command => command.data),
...messageContextMenuInteractions.map(messageContextMenu => messageContextMenu.data),
Expand Down
16 changes: 11 additions & 5 deletions src/tick/giveaway.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, Client, EmbedBuilder, TextChannel } from "discord.js"
import { randomInt } from "node:crypto"
import LogManager from "src/logger/logger"
import { AsyncDatabase } from "src/sqlite/sqlite"
import LogManager from "../logger/logger"
import { AsyncDatabase } from "../sqlite/sqlite"

type giveawayStatus = 0 | 1
const GIVEAWAY_STATUS: { OPENED: giveawayStatus, CLOSED: giveawayStatus } = { OPENED: 0, CLOSED: 1 }

export default async function (client: Client, db: AsyncDatabase, log: LogManager): Promise<void> {
export default async function (client: Client): Promise<void> {
const logger = LogManager.getInstance().logger('GiveawayTick')
const db = await AsyncDatabase.open()
if (!db) {
await logger.log('ERROR', 'Could not open database')
return
}
const giveawayArr = await db.allAsync(`SELECT message_id, prize, timestamp, status, organizer_id, channel_id FROM giveaways WHERE status = 0 and channel_id is not null`, [])
for (const giveawayObj of giveawayArr) {
const timestamp = giveawayObj.timestamp
if (timestamp < new Date().getTime()) {
await log.logger('giveaway-background').log('DEBUG', `Found due giveaway ${giveawayObj.message_id as string}`)
await logger.log('DEBUG', `Found due giveaway ${giveawayObj.message_id as string}`)
const allTeilnehmerArr = await db.allAsync(`SELECT giveaway_message_id, dc_id FROM giveaway_participants WHERE giveaway_message_id = ?`, [giveawayObj.message_id])
const winner = allTeilnehmerArr.length > 0 ? allTeilnehmerArr[randomInt(allTeilnehmerArr.length)]?.dc_id as string : 'niemand'
await log.logger('giveaway-background').log('DEBUG', `giveaway ${giveawayObj.message_id as string} has winner ${winner}`)
await logger.log('DEBUG', `giveaway ${giveawayObj.message_id as string} has winner ${winner}`)
await db.runAsync(`UPDATE giveaways SET status = ? WHERE message_id = ? AND organizer_id = ?`, [GIVEAWAY_STATUS.CLOSED, giveawayObj.message_id, giveawayObj.organizer_id])
await db.runAsync(`DELETE FROM giveaways WHERE message_id = ? AND organizer_id = ? AND status = ?`, [giveawayObj.message_id, giveawayObj.organizer_id, GIVEAWAY_STATUS.CLOSED])
await db.runAsync(`DELETE FROM giveaway_participants WHERE giveaway_message_id = ?`, [giveawayObj.message_id])
Expand Down

0 comments on commit 62b9626

Please sign in to comment.