Skip to content

Commit 121682b

Browse files
feature: giveaway refactor
1 parent fdf227f commit 121682b

25 files changed

+602
-223
lines changed

.env.example

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
DISCORD_TOKEN="XXX"
2-
OWNERS=XXX
1+
DISCORD_TOKEN="bot token from https://discord.com/developers/applications"
2+
OWNERS=put your user id here
33

4-
GIVEAWAY_DATABASE_NAME=GiveawaysDB
54
DEV=true
6-
7-
# DP_SPOTIFY_CLIENT_ID=
8-
# DP_SPOTIFY_CLIENT_SECRET=

.vscode/launch.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99
"cwd": "${workspaceRoot}",
1010
"runtimeExecutable": "yarn",
1111
"runtimeArgs": ["dev:piece"],
12+
},
13+
{
14+
"type": "node",
15+
"request": "launch",
16+
"name": "dev debug",
17+
"protocol": "inspector",
18+
"cwd": "${workspaceRoot}",
19+
"runtimeExecutable": "yarn",
20+
"runtimeArgs": ["dev"],
1221
}
1322
]
1423
}

@types/discord/messaging.d.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

@types/jobs.d.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

development/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@
44
// --- RECCOMMENDED TO KEEP THESE AT ALL TIMES
55
// use logger
66
import { ILogger } from '@sapphire/framework';
7-
globalThis.logger = { ...console, debug: console.info } as unknown as ILogger;
7+
globalThis.logger = {
8+
...console,
9+
debug: console.info,
10+
write: console.log,
11+
} as unknown as ILogger;
812
// use environment variables
913
import { resolve } from 'node:path';
1014
import { setup } from '@skyra/env-utilities';
15+
import { DB } from '@/database/database';
1116
const DotenvConfigOutput = setup({ path: resolve(__dirname, '..', '.env') });
1217
console.log('DOTENV_CONFIG_OUTPUT:', DotenvConfigOutput, '\n\n');
1318
// --- Start custom code
14-
import { GetGiveaways } from '@/services/giveaway';
1519

1620
async function main() {
17-
const a = await GetGiveaways();
18-
console.log(a);
21+
DB;
22+
await new Promise((resolve) => setTimeout(resolve, 10_000));
1923
return;
2024
}
2125

src/commands/giveaways/fetch-giveaways.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getTextBasedChannel } from '#lib/discord-fetch';
2-
import { FetchAndSendGiveaways } from '@/services/giveaway';
2+
import { GiveawayService } from '@/services/giveaway/giveaway-service';
33
import { GiveawayStatusEnum } from '@/services/giveaway/giveaway-status';
44
import { Command } from '@sapphire/framework';
55
import { PermissionsBitField } from 'discord.js';
@@ -20,16 +20,18 @@ export class FetchGiveawaysCommand extends Command {
2020
.setDescription(this.description)
2121
.addBooleanOption((option) =>
2222
option
23-
.setName('force')
24-
.setDescription('Does not check if previously sent')
23+
.setName('unfiltered')
24+
.setDescription(
25+
'Does not check if giveaways have been previously sent'
26+
)
2527
);
2628
});
2729
}
2830

2931
public override async chatInputRun(
3032
interaction: Command.ChatInputCommandInteraction
3133
) {
32-
// const force = interaction.options.getBoolean('force') ?? false;
34+
const unfiltered = interaction.options.getBoolean('unfiltered') ?? false;
3335
await interaction.deferReply();
3436

3537
const { channelId } = interaction;
@@ -40,9 +42,13 @@ export class FetchGiveawaysCommand extends Command {
4042
}
4143

4244
await interaction.editReply('Will search for new giveaways');
43-
const giveawayStatus = await FetchAndSendGiveaways(channel);
45+
let giveawayService = await new GiveawayService().initialize();
46+
if (!unfiltered) {
47+
giveawayService = await giveawayService.filterGiveaways(channel);
48+
}
4449

45-
if (giveawayStatus.status === GiveawayStatusEnum.SUCCESS) {
50+
const giveawayStatus = await giveawayService.sendGiveaways(channel);
51+
if (giveawayStatus.statusCode === GiveawayStatusEnum.SUCCESS) {
4652
return interaction.followUp('Giveaway fetch finished!');
4753
}
4854
return interaction.editReply(giveawayStatus.statusInformation.log_message);

src/commands/giveaways/subscribe-to-giveaways.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { getTextBasedChannel } from '#lib/discord-fetch';
1+
import { getChannelParentID, getTextBasedChannel } from '#lib/discord-fetch';
22
import { Command } from '@sapphire/framework';
33
import { PermissionsBitField } from 'discord.js';
44
import cronstrue from 'cronstrue';
55
import { GiveawayNotifier } from '../../jobs/giveaways';
6+
import { DB } from '@/database/database';
67

78
export class SubscribeToGiveawaysCommand extends Command {
89
public constructor(context: Command.LoaderContext, options: Command.Options) {
@@ -27,25 +28,27 @@ export class SubscribeToGiveawaysCommand extends Command {
2728
) {
2829
await interaction.deferReply();
2930

30-
const { channelId } = interaction;
31+
const { client, channelId } = interaction;
3132
const channel =
3233
interaction.channel ?? (await getTextBasedChannel(channelId));
3334
if (!channel) {
3435
return interaction.editReply('Error: Channel not found');
3536
}
3637

37-
return interaction.editReply('WIP');
38-
39-
// const store = client.sqlStores.GiveawayChannel;
40-
// await store.saveChannel(channel);
38+
const channelParentId = getChannelParentID(channel);
39+
const query = DB.replaceInto('channel_purposes').values({
40+
channel_container: channelParentId.id,
41+
channel_id: channel.id,
42+
channel_purpose: 'givaways',
43+
});
44+
await query.execute();
4145

42-
// const { discordTime, toUnixTimecode } = client.utils.date;
43-
// const nextUnixTimecode = toUnixTimecode(GiveawayNotifier.cron.next());
46+
const { discordTime, toUnixTimecode } = client.utils.date;
47+
const nextUnixTimecode = toUnixTimecode(GiveawayNotifier.cron.next());
4448

45-
// await interaction.editReply(
46-
// `${'This channel is subscribed to giveaways'}\nNext fetch will happen at: ${discordTime(
47-
// nextUnixTimecode
48-
// )}`
49-
// );
49+
return interaction.editReply(
50+
`This channel is subscribed to giveaways\n` +
51+
`Next fetch will happen at ${discordTime(nextUnixTimecode)}`
52+
);
5053
}
5154
}

src/commands/giveaways/unsubscribe-from-giveaways.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { getTextBasedChannel } from '#lib/discord-fetch';
1+
import { getChannelParentID, getTextBasedChannel } from '#lib/discord-fetch';
2+
import { DB } from '@/database/database';
23
import { Command } from '@sapphire/framework';
34
import { PermissionsBitField } from 'discord.js';
45

@@ -29,12 +30,15 @@ export class UnsubscribeToGiveawaysCommand extends Command {
2930
return interaction.editReply('Error: Channel not found');
3031
}
3132

32-
return interaction.editReply('WIP');
33-
// const store = client.sqlStores.GiveawayChannel;
34-
// const result = await store.deleteChannel(channel);
35-
// if (!result.changes) {
36-
// return interaction.editReply(`Channel was not subscribed before`);
37-
// }
38-
// await interaction.editReply(`Successfully unsubscribed`);
33+
const channelParentId = getChannelParentID(channel);
34+
const query = DB.deleteFrom('channel_purposes')
35+
.where('channel_container', '=', channelParentId.id)
36+
.where('channel_id', '=', channel.id);
37+
const result = await query.executeTakeFirst();
38+
39+
if (result.numDeletedRows > 0) {
40+
return interaction.editReply(`Successfully unsubscribed`);
41+
}
42+
return interaction.editReply(`Channel was not subscribed before`);
3943
}
4044
}

src/commands/music/biquad.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import type { APIApplicationCommandOptionChoice } from 'discord.js';
66

77
type SupportedBiquadFilters = keyof typeof BiquadFilterType | 'Off';
88

9-
// TODO: add run `runIn: 'GUILD_TEXT'` to all music commands
109
export class BiquadCommand extends Command {
1110
public constructor(context: Command.LoaderContext, options: Command.Options) {
1211
super(context, {

src/database/database-giveaways.ts

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)