initialized discord bot
This commit is contained in:
@@ -1,17 +1,49 @@
|
||||
{
|
||||
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
|
||||
"parser": "@typescript-eslint/parser",
|
||||
"plugins": ["@typescript-eslint"],
|
||||
"root": true,
|
||||
"extends": "eslint:recommended",
|
||||
"env": {
|
||||
"node": true,
|
||||
"es6": true
|
||||
},
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 2021,
|
||||
"sourceType": "module"
|
||||
"ecmaVersion": 2021
|
||||
},
|
||||
"rules": {
|
||||
"no-console": "off"
|
||||
}
|
||||
"arrow-spacing": ["warn", { "before": true, "after": true }],
|
||||
"brace-style": ["error", "stroustrup", { "allowSingleLine": true }],
|
||||
"comma-dangle": ["error", "always-multiline"],
|
||||
"comma-spacing": "error",
|
||||
"comma-style": "error",
|
||||
"curly": ["error", "multi-line", "consistent"],
|
||||
"dot-location": ["error", "property"],
|
||||
"handle-callback-err": "off",
|
||||
"indent": ["error", "tab"],
|
||||
"keyword-spacing": "error",
|
||||
"max-nested-callbacks": ["error", { "max": 4 }],
|
||||
"max-statements-per-line": ["error", { "max": 2 }],
|
||||
"no-console": "off",
|
||||
"no-empty-function": "error",
|
||||
"no-floating-decimal": "error",
|
||||
"no-inline-comments": "error",
|
||||
"no-lonely-if": "error",
|
||||
"no-multi-spaces": "error",
|
||||
"no-multiple-empty-lines": ["error", { "max": 2, "maxEOF": 1, "maxBOF": 0 }],
|
||||
"no-shadow": ["error", { "allow": ["err", "resolve", "reject"] }],
|
||||
"no-trailing-spaces": ["error"],
|
||||
"no-var": "error",
|
||||
"object-curly-spacing": ["error", "always"],
|
||||
"prefer-const": "error",
|
||||
"quotes": ["error", "single"],
|
||||
"semi": ["error", "always"],
|
||||
"space-before-blocks": "error",
|
||||
"space-before-function-paren": ["error", {
|
||||
"anonymous": "never",
|
||||
"named": "never",
|
||||
"asyncArrow": "always"
|
||||
}],
|
||||
"space-in-parens": "error",
|
||||
"space-infix-ops": "error",
|
||||
"space-unary-ops": "error",
|
||||
"spaced-comment": "error",
|
||||
"yoda": "error"
|
||||
}
|
||||
}
|
||||
|
12
discordbot/index.js
Normal file
12
discordbot/index.js
Normal file
@@ -0,0 +1,12 @@
|
||||
const { Client, Events, GatewayIntentBits } = require('discord.js');
|
||||
const dotenv = require('dotenv');
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
|
||||
|
||||
client.once(Events.ClientReady, c => {
|
||||
console.log(`Ready! Logged in as ${c.user.tag}`);
|
||||
});
|
||||
|
||||
client.login(process.env.DISCORD_TOKEN);
|
4513
discordbot/package-lock.json
generated
4513
discordbot/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -2,26 +2,17 @@
|
||||
"name": "discordbot",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.ts",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"dev": "tsx watch src/index.ts",
|
||||
"start": "node dist/index.js",
|
||||
"build": "tsup src/index.ts --minify",
|
||||
"lint": "eslint \"**/*.{ts, tsx}\"",
|
||||
"eslint-fix": "eslint . --fix"
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"author": "Xeovalyte",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"discord.js": "^14.11.0",
|
||||
"discord.js": "^14.12.1",
|
||||
"dotenv": "^16.3.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@typescript-eslint/eslint-plugin": "^5.60.1",
|
||||
"@typescript-eslint/parser": "^5.60.1",
|
||||
"eslint": "^8.43.0",
|
||||
"tsup": "^7.1.0",
|
||||
"tsx": "^3.12.7",
|
||||
"typescript": "^5.1.3"
|
||||
"eslint": "^8.46.0"
|
||||
}
|
||||
}
|
||||
|
@@ -1,24 +0,0 @@
|
||||
import { ChatInputCommandInteraction, SlashCommandBuilder, Client, PermissionFlagsBits } from "discord.js";
|
||||
import { basicEmbed } from '../createEmbed'
|
||||
|
||||
export const data = new SlashCommandBuilder()
|
||||
.setName("clear")
|
||||
.setDescription("Clear 1-100 messages")
|
||||
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator)
|
||||
.addNumberOption(option => option
|
||||
.setName('amount')
|
||||
.setDescription('The amount of messages to creat')
|
||||
.setRequired(true)
|
||||
)
|
||||
|
||||
export async function execute({ interaction }: { interaction: ChatInputCommandInteraction, client: Client }) {
|
||||
const amount = interaction.options.getNumber('amount');
|
||||
|
||||
if (!amount || !interaction.channel || amount < 1 || amount > 100) return await interaction.reply({ embeds: [basicEmbed('The amount must be between 1-100')] });
|
||||
|
||||
if (interaction.channel.isDMBased()) return await interaction.reply({ embeds: [basicEmbed('This command can only be executed inside a guild')], ephemeral: true })
|
||||
|
||||
interaction.channel.bulkDelete(amount)
|
||||
|
||||
await interaction.reply({ embeds: [basicEmbed(`Cleared **${amount}** messages`)], ephemeral: true })
|
||||
}
|
@@ -1,11 +0,0 @@
|
||||
import { ChatInputCommandInteraction, SlashCommandBuilder, Client } from "discord.js";
|
||||
import { basicEmbed } from '../createEmbed'
|
||||
|
||||
export const data = new SlashCommandBuilder()
|
||||
.setName("ping")
|
||||
.setDescription("Replies with Pong!");
|
||||
|
||||
export async function execute({ interaction, client }: { interaction: ChatInputCommandInteraction, client: Client }) {
|
||||
const reply = await interaction.reply({ embeds: [basicEmbed(`Websocket heartbeat: **${client.ws.ping}ms**\n Roundtrip latency: **Pinging...**`)], fetchReply: true, ephemeral: true });
|
||||
interaction.editReply({ embeds: [basicEmbed(`Websocket heartbeat: **${client.ws.ping}ms**\n Roundtrip latency: **${reply.createdTimestamp - interaction.createdTimestamp}ms**`)] });
|
||||
}
|
@@ -1,20 +0,0 @@
|
||||
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
|
||||
};
|
||||
|
@@ -1,8 +0,0 @@
|
||||
import { EmbedBuilder } from 'discord.js'
|
||||
import { config } from './config'
|
||||
|
||||
export const basicEmbed = (description: string) => {
|
||||
return new EmbedBuilder()
|
||||
.setColor(config.EMBED_COLOR)
|
||||
.setDescription(description);
|
||||
};
|
@@ -1,23 +0,0 @@
|
||||
import { Events, Client, BaseInteraction } from 'discord.js'
|
||||
|
||||
export const name = Events.InteractionCreate
|
||||
|
||||
export const execute = async ({ client }: { client: Client }, 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 });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,14 +0,0 @@
|
||||
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 }) => {
|
||||
if (!client.user) throw Error('No client.user')
|
||||
|
||||
console.log(`Ready! Logged in as ${client.user.tag}`)
|
||||
|
||||
registerCommands()
|
||||
}
|
@@ -1,48 +0,0 @@
|
||||
import { Client, GatewayIntentBits, Collection } from 'discord.js'
|
||||
import fs from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import { config } from './config'
|
||||
|
||||
type Command = {
|
||||
data: any
|
||||
}
|
||||
|
||||
const client = new Client({
|
||||
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent]
|
||||
})
|
||||
|
||||
|
||||
client.commands = new Collection()
|
||||
|
||||
const commandsPath = path.join(__dirname, 'commands');
|
||||
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.ts'));
|
||||
|
||||
(async () => {
|
||||
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) {
|
||||
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'));
|
||||
|
||||
(async () => {
|
||||
for (const file of eventFiles) {
|
||||
const filePath = path.join(eventsPath, file);
|
||||
const event = await import(filePath);
|
||||
if (event.once) {
|
||||
client.once(event.name, (...args) => event.execute({ client }, ...args));
|
||||
} else {
|
||||
client.on(event.name, (...args) => event.execute({ client }, ...args));
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
client.login(config.DISCORD_TOKEN);
|
@@ -1,54 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
@@ -1,18 +0,0 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2020",
|
||||
"module": "commonjs",
|
||||
"rootDir": "./src",
|
||||
"outDir": "./dist",
|
||||
"removeComments": true,
|
||||
"resolveJsonModule": true,
|
||||
"esModuleInterop": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"strict": true,
|
||||
"strictNullChecks": true,
|
||||
"skipLibCheck": true,
|
||||
"compilerOptions": {
|
||||
"module": "commonjs"
|
||||
}
|
||||
}
|
||||
}
|
11
discordbot/types.d.ts
vendored
11
discordbot/types.d.ts
vendored
@@ -1,11 +0,0 @@
|
||||
import { Collection } from 'discord.js'
|
||||
|
||||
declare module 'discord.js' {
|
||||
export interface Client {
|
||||
commands: Collection<unknown, any>
|
||||
}
|
||||
}
|
||||
|
||||
declare global {
|
||||
type CreateEmbed = (message: string) => void
|
||||
}
|
Reference in New Issue
Block a user