Skip to content

Commit

Permalink
feat: temp voice and giveaway
Browse files Browse the repository at this point in the history
  • Loading branch information
louiszn committed Oct 21, 2024
1 parent 8b5f430 commit a9c9832
Show file tree
Hide file tree
Showing 11 changed files with 533 additions and 58 deletions.
Binary file modified bun.lockb
Binary file not shown.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
"devDependencies": {
"@types/bun": "latest",
"@types/humanize-duration": "^3.27.4",
"@types/ms": "^0.7.34",
"prettier": "^3.3.3"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"cron": "^3.1.7",
"discord.js": "^14.16.3",
"humanize-duration": "^3.32.1",
"ioredis": "^5.4.1",
"minisearch": "^7.1.0",
"mongoose": "^8.7.0"
"mongoose": "^8.7.0",
"ms": "^2.1.3"
}
}
115 changes: 115 additions & 0 deletions src/commands/modules/Giveaway.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import {
EmbedBuilder,
PermissionFlagsBits,
SlashCommandBuilder,
} from "discord.js";
import ms from "ms";
import Command from "../Command";

export default class extends Command {
public constructor() {
super("giveaway");

this.applicationCommands.push(
new SlashCommandBuilder()
.setName("giveaway")
.setDescription("Module giveaway")
.setDefaultMemberPermissions(PermissionFlagsBits.ManageMessages)
.addSubcommand((subcommand) =>
subcommand
.setName("start")
.setDescription("Bắt đầu một đợt giveaway mới")
.addStringOption((option) =>
option
.setName("prize")
.setDescription("Tên giveaway")
.setRequired(true)
.setMaxLength(50),
)
.addStringOption((option) =>
option
.setName("duration")
.setDescription(
"Thời gian giveaway kết thúc (5d, 5h, 5m, 5s, ...)",
)
.setRequired(true),
)
.addIntegerOption((option) =>
option
.setName("winners")
.setDescription("Số lượng người thắng giveaway")
.setRequired(true)
.setMinValue(1)
.setMaxValue(15),
),
)
.toJSON(),
);

this.subcommands["giveaway"] = [
{
name: "start",
target: "start",
},
];
}

protected async _start(interaction: Command.ChatInput) {
const { channel, guildId, user, options, client } = interaction;
const { config, modules } = client;

const prize = options.getString("prize", true);
const duration: number | undefined = ms(
options.getString("duration", true),
);
const winners = options.getInteger("winners", true);

if (typeof duration === "undefined") {
await interaction.reply({
embeds: [
new EmbedBuilder()
.setDescription(
"❌ Cú pháp thời gian không hợp lệ (5d, 5h, 5m, 5s).",
)
.setColor(config.colors.error),
],
ephemeral: true,
});

return;
}

if (duration < 5_000 || duration >= 30 * 24 * 60 * 60 * 1_000) {
await interaction.reply({
embeds: [
new EmbedBuilder()
.setDescription(
"❌ Thời gian tối thiểu là 5 giây và tối đa là 30 ngày.",
)
.setColor(config.colors.error),
],
ephemeral: true,
});

return;
}

await modules.giveaway.create(
guildId,
channel!.id,
user.id,
prize,
winners,
new Date(Date.now() + duration),
);

await interaction.reply({
embeds: [
new EmbedBuilder()
.setDescription("✅ Đã tạo giveaway mới.")
.setColor(config.colors.default),
],
ephemeral: true,
});
}
}
85 changes: 85 additions & 0 deletions src/components/modules/Giveaway.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import {
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
EmbedBuilder,
} from "discord.js";
import Component from "../Component";

export default class Giveaway extends Component {
static joinButton(disabled = false, amount = 0) {
return new ButtonBuilder()
.setCustomId(`giveaway|join`)
.setLabel(`Tham gia (${amount})`)
.setEmoji("🎉")
.setStyle(ButtonStyle.Secondary)
.setDisabled(disabled);
}

public constructor() {
super("giveaway");
}

public override async executeButton(
interaction: Component.Button,
args: string[],
) {
const { channelId, client, message, user } = interaction;
const { modules, config } = client;

const [choice] = args;

switch (choice) {
case "join": {
const [status, userCount] = await modules.giveaway.passUser(
channelId,
message.id,
user.id,
);

if (status === -1) {
await interaction.reply({
embeds: [
new EmbedBuilder()
.setDescription(
"❌ Giveaway này không có trong dữ liệu của tớ.",
)
.setColor(config.colors.error),
],
ephemeral: true,
});

return;
}

if (status === 0) {
await interaction.reply({
embeds: [
new EmbedBuilder()
.setDescription("✅ Đã rời khỏi giveaway này!")
.setColor(config.colors.default),
],
ephemeral: true,
});
} else {
await interaction.reply({
embeds: [
new EmbedBuilder()
.setDescription("✅ Đã tham gia giveaway này!")
.setColor(config.colors.default),
],
ephemeral: true,
});
}

await message.edit({
components: [
new ActionRowBuilder<any>().setComponents(
Giveaway.joinButton(false, userCount),
),
],
});
}
}
}
}
15 changes: 15 additions & 0 deletions src/events/modules/Ready.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Client } from "discord.js";
import Listener from "../Listener";

export default class extends Listener {
public constructor() {
super("ready");
}

public override async execute(client: Client<true>) {
const { modules } = client;

modules.giveaway.start();
modules.tempVoice.start();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ChannelType, type GuildChannelCreateOptions, type VoiceState } from "discord.js";

import Listener from "../../Listener";
import TempVoiceCreator from "../../../models/TempVoiceCreator";
import TempVoice from "../../../models/TempVoice";
import Listener from "../Listener";
import TempVoiceCreator from "../../models/TempVoiceCreator";
import TempVoice from "../../models/TempVoice";

import { sleep } from "bun";

Expand Down
54 changes: 0 additions & 54 deletions src/events/modules/TempVoice/Ready.ts

This file was deleted.

3 changes: 3 additions & 0 deletions src/managers/ModuleManager.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import type { Client } from "discord.js";
import TempVoiceManager from "./modules/TempVoiceManager";
import GiveawayManager from "./modules/GiveawayManager";

export default class ModuleManager {
public tempVoice: TempVoiceManager;
public giveaway: GiveawayManager;

public constructor(client: Client<true>) {
this.tempVoice = new TempVoiceManager(client);
this.giveaway = new GiveawayManager(client);
}
}
Loading

0 comments on commit a9c9832

Please sign in to comment.