Initialized discord bot version 2
Some checks failed
Build and Deploy / Deploy Web (push) Failing after 1s
Build and Deploy / Deploy Discord Bot (push) Failing after 1s

This commit is contained in:
Xeovalyte 2023-06-27 19:38:28 +02:00
parent f5d9aefc0f
commit d52431ffb5
11 changed files with 4043 additions and 0 deletions

2
discordbot/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.env
node_modules

3833
discordbot/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

22
discordbot/package.json Normal file
View File

@ -0,0 +1,22 @@
{
"name": "discordbot",
"version": "1.0.0",
"description": "",
"main": "index.ts",
"scripts": {
"dev": "tsx watch src/index.ts",
"start": "node dist/index.js",
"build": "tsup src/index.ts --minify"
},
"author": "",
"license": "ISC",
"dependencies": {
"discord.js": "^14.11.0",
"dotenv": "^16.3.1"
},
"devDependencies": {
"tsup": "^7.1.0",
"tsx": "^3.12.7",
"typescript": "^5.1.3"
}
}

View File

@ -0,0 +1,10 @@
import { CommandInteraction, SlashCommandBuilder, Client } from "discord.js";
export const data = new SlashCommandBuilder()
.setName("ping")
.setDescription("Replies with Pong!");
export async function execute({ interaction, client, createEmbed }: { interaction: CommandInteraction, client: Client, createEmbed: Function }) {
const reply = await interaction.reply({ embeds: [createEmbed(`Websocket heartbeat: **${client.ws.ping}ms**\n Roundtrip latency: **Pinging...**`)], fetchReply: true, ephemeral: true });
interaction.editReply({ embeds: [createEmbed(`Websocket heartbeat: **${client.ws.ping}ms**\n Roundtrip latency: **${reply.createdTimestamp - interaction.createdTimestamp}ms**`)] });
}

20
discordbot/src/config.ts Normal file
View File

@ -0,0 +1,20 @@
import { ColorResolvable } from "discord.js";
import dotenv from "dotenv";
dotenv.config();
const { DISCORD_TOKEN, DISCORD_CLIENT_ID, DISCORD_GUILD_ID } = process.env;
if (!DISCORD_TOKEN || !DISCORD_CLIENT_ID) {
throw new Error("Missing environment variables");
}
const EMBED_COLOR: ColorResolvable = '#0080ff'
export const config = {
DISCORD_TOKEN,
DISCORD_CLIENT_ID,
DISCORD_GUILD_ID,
EMBED_COLOR
};

View File

@ -0,0 +1,23 @@
import { Events, Client, BaseInteraction } from 'discord.js'
export const name = Events.InteractionCreate
export const execute = async ({ client, createEmbed }: { client: Client, createEmbed: Function }, interaction: BaseInteraction) => {
if (interaction.isChatInputCommand()) {
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
return console.error(`No command matching ${interaction.commandName} was found`);
} else {
console.info(`${interaction.user.username} executed command ${interaction.commandName}`);
}
try {
await command.execute({ interaction, client, createEmbed });
} catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
}
}

View File

@ -0,0 +1,12 @@
import { Events, Client } from 'discord.js'
import registerCommands from '../registerCommands'
export const name = Events.ClientReady
export const once = true
export const execute = async ({ client }: { client: Client }) => {
console.log(`Ready! Logged in as ${client.user.tag}`)
registerCommands()
}

45
discordbot/src/index.ts Normal file
View File

@ -0,0 +1,45 @@
import { Client, GatewayIntentBits, Collection, EmbedBuilder } from 'discord.js'
import fs from 'node:fs'
import path from 'node:path'
import { config } from './config'
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent]
})
const createEmbed = (description: string) => {
return new EmbedBuilder()
.setColor(config.EMBED_COLOR)
.setDescription(description);
};
client.commands = new Collection()
const commandsPath = path.join(__dirname, 'commands');
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.ts'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
} else {
console.error(`The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.ts'));
for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
const event = require(filePath);
if (event.once) {
client.once(event.name, (...args) => event.execute({ client, createEmbed }, ...args));
} else {
client.on(event.name, (...args) => event.execute({ client, createEmbed }, ...args));
}
}
client.login(config.DISCORD_TOKEN);

View File

@ -0,0 +1,54 @@
import fs from 'node:fs'
import path from 'node:path'
import { REST, Routes } from 'discord.js'
import { config } from './config'
type Command = {
data: any
}
export default async () => {
const commands = []
const commandsPath = path.join(__dirname, 'commands')
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('ts'))
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file)
const command = await import(filePath) as Command
if ('data' in command && 'execute' in command) {
commands.push(command.data.toJSON())
} else {
console.error(`The command at ${filePath} is missing a required "data" or "execute" property`)
}
}
const rest = new REST().setToken(config.DISCORD_TOKEN)
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);
// The put method is used to fully refresh all commands in the guild with the current set
if (config.DISCORD_GUILD_ID) {
const data: any = await rest.put(
Routes.applicationGuildCommands(config.DISCORD_CLIENT_ID, config.DISCORD_GUILD_ID),
{ body: commands },
);
console.log(`Successfully reloaded ${data.length} guild (/) commands.`);
} else {
const data: any = await rest.put(
Routes.applicationCommands(config.DISCORD_CLIENT_ID),
{ body: commands },
);
console.log(`Successfully reloaded ${data.length} global (/) commands.`);
}
} catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error);
}
}

15
discordbot/tsconfig.json Normal file
View File

@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"rootDir": "./src",
"outDir": "./dist",
"removeComments": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"strictNullChecks": true,
"skipLibCheck": true
}
}

7
discordbot/types.d.ts vendored Normal file
View File

@ -0,0 +1,7 @@
import { Collection } from 'discord.js'
declare module 'discord.js' {
export interface Client {
commands: Collection<unknown, any>
}
}