Skip to content

Commit

Permalink
Enhance commands and clean up (#14)
Browse files Browse the repository at this point in the history
* Update gunun sorusu flow

update gunun sorusu flow

* update for dev env

* wip

* wip

* enhance commands

---------

Co-authored-by: Can Sirin <[email protected]>
  • Loading branch information
cansirin and cansirin authored Dec 23, 2023
1 parent e0d9fe4 commit 74e1a14
Show file tree
Hide file tree
Showing 17 changed files with 297 additions and 177 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
CLIENT_ID=client-id
GUILD_ID=guild-id
DISCORD_TOKEN=discord-token
CHATGPT_API_KEY=get-your-chatgpt-key
GIDENLER_CHANNEL_ID=channel-id
GIDENLER_VIDEO_LINK=youtube-link
SORU_CEVAP_CHANNEL_ID=channel-id
GUNAYDIN_CHANNEL_ID=channel-id
15 changes: 15 additions & 0 deletions .github/workflows/fly-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Fly deploy
on:
push:
branches:
- dev

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: superfly/flyctl-actions/setup-flyctl@master
- run: flyctl deploy --app bot-dev --remote-only
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
2 changes: 1 addition & 1 deletion .github/workflows/fly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: superfly/flyctl-actions/setup-flyctl@master
- run: flyctl deploy --remote-only
- run: flyctl deploy --app kampus-discord-bot --remote-only
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
3 changes: 0 additions & 3 deletions config.ts

This file was deleted.

7 changes: 5 additions & 2 deletions deploy-commands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { REST, Routes } from "discord.js";
import { readdirSync } from "fs";
import { KAMPUS_GUILD_ID } from "./config";
import dotenv from "dotenv";

dotenv.config();
Expand All @@ -18,17 +17,21 @@ for (const file of commandFiles) {
if (process.env.DISCORD_TOKEN === undefined) {
throw new Error("DISCORD_TOKEN is undefined");
}

const rest = new REST({ version: "10" }).setToken(process.env.DISCORD_TOKEN);

(async (env) => {
if (process.env.CLIENT_ID === undefined) {
throw new Error("DISCORD_TOKEN is undefined");
}
if (process.env.GUILD_ID === undefined) {
throw new Error("GUILD_ID is undefined");
}
try {
console.log("Started refreshing application (/) commands.");
if (env === "production") {
console.log("Started refreshing guild (/) commands.");
await rest.put(Routes.applicationGuildCommands(process.env.CLIENT_ID, KAMPUS_GUILD_ID), {
await rest.put(Routes.applicationGuildCommands(process.env.CLIENT_ID, process.env.GUILD_ID), {
body: commands,
});
}
Expand Down
23 changes: 23 additions & 0 deletions fly.dev.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# fly.toml app configuration file generated for kampus-discord-bot on 2023-11-26T10:45:21-08:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = "bot-dev"
primary_region = "sjc"

[build]
dockerfile = "Dockerfile"

[http_service]
internal_port = 3000
force_https = true
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 1
processes = ["app"]

[[vm]]
cpu_kind = "shared"
cpus = 1
memory_mb = 512
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"scripts": {
"start": "nodemon index.ts",
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "tsx ./index.ts"
"dev": "tsx ./index.ts",
"set-secrets-dev": "flyctl secrets set --app bot-dev",
"set-secrets": "flyctl secrets set --app kampus-discord-bot"
},
"keywords": [],
"author": "",
Expand Down
17 changes: 5 additions & 12 deletions src/commands/assign-role.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { Client, CommandInteraction, SlashCommandBuilder } from "discord.js";
// @ts-ignore
import members from "../../grouped.json" assert { type: "json" };

export async function assignRole(client: Client, guildID: string, userID: string) {
const guild = client.guilds.cache.get(guildID); // Replace with your guild ID
if (!guild) {
console.error("Guild not found.");
return;
}
if (!members) {
console.error("You should run get-all-members first");
return;
}

try {
const role = guild.roles.cache.find((r) => r.name === "v2"); // Replace with the role name
Expand All @@ -36,7 +30,8 @@ export async function assignRole(client: Client, guildID: string, userID: string
const assignRoleCommand = {
data: new SlashCommandBuilder()
.setName("assign-role")
.setDescription("Assignes a role to a user"),
.setDescription("Assignes a role to a user")
.addUserOption((option) => option.setName("user").setDescription("User to assign role to")),
async execute(interaction: CommandInteraction) {
const client = interaction.client;
const guildID = interaction.guildId;
Expand All @@ -45,12 +40,10 @@ const assignRoleCommand = {
return;
}

for (const member of Object.keys(members)) {
await assignRole(client, guildID, member);
}
const member = interaction.options.getUser("user");

console.log("Assigned roles so far: ", Object.keys(members).length);
console.log("Finished assigning roles.");
await assignRole(client, guildID, member?.id ?? "");
console.log("Finished assigning role.");
},
};

Expand Down
107 changes: 77 additions & 30 deletions src/commands/daily-morning-question.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,83 @@
import { CommandInteraction, Constants, SlashCommandBuilder, TextChannel } from "discord.js";
import { GUNAYDIN_CHANNEL_ID } from "../../config";
import _ from "lodash";
import {
CommandInteraction,
Message,
SlashCommandBuilder,
TextChannel,
messageLink,
} from "discord.js";
import { fetchMessages } from "../features/fetch-messages";
import { isTodayDailyQuestionFound } from "../features/is-daily-msg-exist";

module.exports = {
const dailyMorningQuestion = {
data: new SlashCommandBuilder()
.setName("gunun-sorusu")
.setDescription("Günün sorusunu oluşturur."),
.setDescription("Creates 'Günün sorusu' thread.")
.addNumberOption((option) => option.setName("count").setDescription("Günün sorusu sayısı")),
async execute(interaction: CommandInteraction) {
const channel = (await interaction.client.channels.fetch(GUNAYDIN_CHANNEL_ID)) as TextChannel;

const today = new Date();
const options = {
month: "long",
day: "numeric",
} as Intl.DateTimeFormatOptions;
const formattedDate = new Intl.DateTimeFormat("tr-TR", options).format(today);

const thread = await channel.threads.create({
name: "Günün Sorusu - " + formattedDate,
});

const geyik = `<:geyik:1161771822350602260>`;
const threadMention = `<#${thread.id}>`;
const message = await channel.send(threadMention);
const threadMessage = await thread.send(
`Volkan abi ve geyikler günün sorusunu soruyor!\n ${geyik} ${geyik} ${geyik} ${geyik} ${geyik} ${geyik}`
);
const messagePin = await message.pin();
Promise.all([threadMessage, messagePin, message]);

interaction.reply({
content: `Günün sorusu başarıyla oluşturuldu! Thread icin: ${threadMention}`,
ephemeral: true,
});
if (!process.env.GUNAYDIN_CHANNEL_ID) {
throw new Error("GUNAYDIN_CHANNEL_ID environment variable is not set.");
}

const channel = (await interaction.client.channels.fetch(
process.env.GUNAYDIN_CHANNEL_ID ?? ""
)) as TextChannel;
const botId = "1183616536682958899";

let messages = (await fetchMessages(
interaction.client,
process.env.GUNAYDIN_CHANNEL_ID,
1,
botId
)) as Message[];

if (!messages) {
console.log("No messages found sent by the bot.");
messages = [];
}

const [isFound, m, c] = isTodayDailyQuestionFound(messages);
const count = (interaction.options.get("count")?.value as number) ?? c;

if (isFound && m) {
await interaction.deferReply({
ephemeral: true,
});
await interaction.editReply({
content: `Günün sorusu zaten oluşturulmuş! ${messageLink(
m.channelId,
m.id,
m.guildId ?? ""
)}`,
});
} else {
const questionTitle = `Günün Sorusu - ${count}`;
const threadMention = buildDailyMorningQuestion(channel, questionTitle);
await interaction.deferReply({
ephemeral: true,
});
await interaction.editReply({
content: `Günün sorusu başarıyla oluşturuldu! Thread icin: ${threadMention}`,
});
}
},
};

export default dailyMorningQuestion;

const buildDailyMorningQuestion = async (channel: TextChannel, questionTitle: string) => {
const thread = await channel.threads.create({
name: questionTitle,
});

const geyik = `<:geyik:1161771822350602260>`;
const threadMention = `<#${thread.id}>`;
const message = await channel.send(threadMention);
const threadMessage = await thread.send(
`Volkan abi ve geyikler günün sorusunu soruyor!\n ${geyik} ${geyik} ${geyik} ${geyik} ${geyik} ${geyik}`
);
const messagePin = await message.pin();
Promise.all([threadMessage, messagePin, message]);

return threadMention;
};
1 change: 1 addition & 0 deletions src/commands/get-all-members.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export async function fetchAllMembers(client: Client, guildID: string) {
});
console.log("Finished fetching members. Total members:", membersCollected.length);
writeToFile(membersCollected);
return membersCollected;
} catch (error) {
console.error("Error fetching members:", error);
}
Expand Down
Loading

0 comments on commit 74e1a14

Please sign in to comment.