mirror of
https://github.com/discordeno/discordeno.git
synced 2026-09-17 08:47:22 +00:00
cleanup cleanup cleanup on isle dd (#2792)
* cleanup cleanup cleanup on isle dd * fix: rest manager import in test
This commit is contained in:
+12
-14
@@ -1,7 +1,5 @@
|
||||
import { configs } from './configs.ts.js';
|
||||
import type {
|
||||
BotWithCache,
|
||||
BotWithHelpersPlugin} from './deps.ts.js';
|
||||
import { configs } from './configs.ts.js'
|
||||
import type { BotWithCache, BotWithHelpersPlugin } from './deps.ts.js'
|
||||
import {
|
||||
Collection,
|
||||
createBot,
|
||||
@@ -10,8 +8,8 @@ import {
|
||||
enableHelpersPlugin,
|
||||
enablePermissionsPlugin,
|
||||
GatewayIntents,
|
||||
} from './deps.ts.js';
|
||||
import type { Command } from './src/types/commands.ts.js';
|
||||
} from './deps.ts.js'
|
||||
import type { Command } from './src/types/commands.ts.js'
|
||||
|
||||
// MAKE THE BASIC BOT OBJECT
|
||||
const bot = createBot({
|
||||
@@ -19,19 +17,19 @@ const bot = createBot({
|
||||
botId: configs.botId,
|
||||
intents: GatewayIntents.Guilds,
|
||||
events: {},
|
||||
});
|
||||
})
|
||||
|
||||
// ENABLE ALL THE PLUGINS THAT WILL HELP MAKE IT EASIER TO CODE YOUR BOT
|
||||
enableHelpersPlugin(bot);
|
||||
enableCachePlugin(bot);
|
||||
enableCacheSweepers(bot as BotWithCache);
|
||||
enablePermissionsPlugin(bot as BotWithCache);
|
||||
enableHelpersPlugin(bot)
|
||||
enableCachePlugin(bot)
|
||||
enableCacheSweepers(bot as BotWithCache)
|
||||
enablePermissionsPlugin(bot as BotWithCache)
|
||||
|
||||
export interface BotClient extends BotWithCache<BotWithHelpersPlugin> {
|
||||
commands: Collection<string, Command>;
|
||||
commands: Collection<string, Command>
|
||||
}
|
||||
|
||||
// THIS IS THE BOT YOU WANT TO USE EVERYWHERE IN YOUR CODE! IT HAS EVERYTHING BUILT INTO IT!
|
||||
export const Bot = bot as BotClient;
|
||||
export const Bot = bot as BotClient
|
||||
// PREPARE COMMANDS HOLDER
|
||||
Bot.commands = new Collection();
|
||||
Bot.commands = new Collection()
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
import { dotEnvConfig } from './deps.ts.js';
|
||||
import { dotEnvConfig } from './deps.ts.js'
|
||||
|
||||
// Get the .env file that the user should have created, and get the token
|
||||
const env = dotEnvConfig({ export: true, path: "./.env" });
|
||||
const token = env.BOT_TOKEN || "";
|
||||
const env = dotEnvConfig({ export: true, path: './.env' })
|
||||
const token = env.BOT_TOKEN || ''
|
||||
|
||||
export interface Config {
|
||||
token: string;
|
||||
botId: bigint;
|
||||
token: string
|
||||
botId: bigint
|
||||
}
|
||||
|
||||
export const configs = {
|
||||
/** Get token from ENV variable */
|
||||
token,
|
||||
/** Get the BotId from the token */
|
||||
botId: BigInt(atob(token.split(".")[0])),
|
||||
botId: BigInt(atob(token.split('.')[0])),
|
||||
/** The server id where you develop your bot and want dev commands created. */
|
||||
devGuildId: BigInt(env.DEV_GUILD_ID!),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
export * from "https://deno.land/x/discordeno@17.0.0/mod.ts";
|
||||
export * from "https://deno.land/x/discordeno@17.0.0/plugins/mod.ts";
|
||||
export * from 'https://deno.land/x/discordeno@17.0.0/mod.ts'
|
||||
export * from 'https://deno.land/x/discordeno@17.0.0/plugins/mod.ts'
|
||||
|
||||
// Terminal Colors!
|
||||
export * from "https://deno.land/std@0.117.0/fmt/colors.ts";
|
||||
export * from 'https://deno.land/std@0.117.0/fmt/colors.ts'
|
||||
// Get data from .env files
|
||||
export { config as dotEnvConfig } from "https://deno.land/x/dotenv@v3.1.0/mod.ts";
|
||||
export { config as dotEnvConfig } from 'https://deno.land/x/dotenv@v3.1.0/mod.ts'
|
||||
// Database, thx Tri!
|
||||
export { decode as KwikDecode, encode as KwikEncode, Kwik } from "https://deno.land/x/kwik@v1.3.1/mod.ts";
|
||||
export { decode as KwikDecode, encode as KwikEncode, Kwik } from 'https://deno.land/x/kwik@v1.3.1/mod.ts'
|
||||
|
||||
+13
-13
@@ -1,25 +1,25 @@
|
||||
import { startBot } from './deps.ts.js';
|
||||
import log from './src/utils/logger.ts.js';
|
||||
import { fileLoader, importDirectory } from './src/utils/loader.ts.js';
|
||||
import { updateApplicationCommands } from './src/utils/updateCommands.ts.js';
|
||||
import { startBot } from './deps.ts.js'
|
||||
import log from './src/utils/logger.ts.js'
|
||||
import { fileLoader, importDirectory } from './src/utils/loader.ts.js'
|
||||
import { updateApplicationCommands } from './src/utils/updateCommands.ts.js'
|
||||
// setup db
|
||||
import './src/database/mod.ts.js';
|
||||
import { Bot } from './bot.ts.js';
|
||||
import './src/database/mod.ts.js'
|
||||
import { Bot } from './bot.ts.js'
|
||||
|
||||
log.info("Starting bot...");
|
||||
log.info('Starting bot...')
|
||||
|
||||
// Forces deno to read all the files which will fill the commands/inhibitors cache etc.
|
||||
await Promise.all(
|
||||
[
|
||||
"./src/commands",
|
||||
"./src/events",
|
||||
'./src/commands',
|
||||
'./src/events',
|
||||
// "./src/tasks",
|
||||
].map((path) => importDirectory(Deno.realPathSync(path))),
|
||||
);
|
||||
await fileLoader();
|
||||
)
|
||||
await fileLoader()
|
||||
|
||||
// UPDATES YOUR COMMANDS TO LATEST COMMANDS
|
||||
await updateApplicationCommands();
|
||||
await updateApplicationCommands()
|
||||
|
||||
// STARTS THE CONNECTION TO DISCORD
|
||||
await startBot(Bot);
|
||||
await startBot(Bot)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Bot } from '../../bot.ts.js';
|
||||
import type { Command } from '../types/commands.ts.js';
|
||||
import { Bot } from '../../bot.ts.js'
|
||||
import type { Command } from '../types/commands.ts.js'
|
||||
|
||||
export function createCommand(command: Command) {
|
||||
Bot.commands.set(command.name, command);
|
||||
Bot.commands.set(command.name, command)
|
||||
}
|
||||
|
||||
@@ -1,22 +1,18 @@
|
||||
import { ApplicationCommandTypes, InteractionResponseTypes } from '../../deps.ts.js';
|
||||
import { snowflakeToTimestamp } from '../utils/helpers.ts.js';
|
||||
import { createCommand } from './mod.ts.js';
|
||||
import { ApplicationCommandTypes, InteractionResponseTypes } from '../../deps.ts.js'
|
||||
import { snowflakeToTimestamp } from '../utils/helpers.ts.js'
|
||||
import { createCommand } from './mod.ts.js'
|
||||
|
||||
createCommand({
|
||||
name: "ping",
|
||||
description: "Ping the Bot!",
|
||||
name: 'ping',
|
||||
description: 'Ping the Bot!',
|
||||
type: ApplicationCommandTypes.ChatInput,
|
||||
execute: async (Bot, interaction) => {
|
||||
const ping = Date.now() - snowflakeToTimestamp(interaction.id);
|
||||
await Bot.helpers.sendInteractionResponse(
|
||||
interaction.id,
|
||||
interaction.token,
|
||||
{
|
||||
type: InteractionResponseTypes.ChannelMessageWithSource,
|
||||
data: {
|
||||
content: `🏓 Pong! ${ping}ms`,
|
||||
},
|
||||
const ping = Date.now() - snowflakeToTimestamp(interaction.id)
|
||||
await Bot.helpers.sendInteractionResponse(interaction.id, interaction.token, {
|
||||
type: InteractionResponseTypes.ChannelMessageWithSource,
|
||||
data: {
|
||||
content: `🏓 Pong! ${ping}ms`,
|
||||
},
|
||||
);
|
||||
})
|
||||
},
|
||||
});
|
||||
})
|
||||
|
||||
@@ -1,32 +1,32 @@
|
||||
import { Kwik, KwikDecode, KwikEncode } from '../../deps.ts.js';
|
||||
import { logger } from '../utils/logger.ts.js';
|
||||
import { Kwik, KwikDecode, KwikEncode } from '../../deps.ts.js'
|
||||
import { logger } from '../utils/logger.ts.js'
|
||||
|
||||
const log = logger({ name: "DB Manager" });
|
||||
const log = logger({ name: 'DB Manager' })
|
||||
|
||||
log.info("Initializing Database");
|
||||
log.info('Initializing Database')
|
||||
|
||||
const kwik = new Kwik();
|
||||
const kwik = new Kwik()
|
||||
|
||||
// Add BigInt Support
|
||||
kwik.msgpackExtensionCodec.register({
|
||||
type: 0,
|
||||
encode: (object: unknown): Uint8Array | null => {
|
||||
if (typeof object === "bigint") {
|
||||
if (typeof object === 'bigint') {
|
||||
if (object <= Number.MAX_SAFE_INTEGER && object >= Number.MIN_SAFE_INTEGER) {
|
||||
return KwikEncode(parseInt(object.toString(), 10), {});
|
||||
return KwikEncode(parseInt(object.toString(), 10), {})
|
||||
} else {
|
||||
return KwikEncode(object.toString(), {});
|
||||
return KwikEncode(object.toString(), {})
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
return null
|
||||
}
|
||||
},
|
||||
decode: (data: Uint8Array) => {
|
||||
return BigInt(KwikDecode(data, {}) as string);
|
||||
return BigInt(KwikDecode(data, {}) as string)
|
||||
},
|
||||
});
|
||||
})
|
||||
|
||||
// Initialize the Database
|
||||
await kwik.init();
|
||||
await kwik.init()
|
||||
|
||||
log.info("Database Initialized!");
|
||||
log.info('Database Initialized!')
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
import { Bot } from '../../bot.ts.js';
|
||||
import { InteractionTypes } from '../../deps.ts.js';
|
||||
import log from '../utils/logger.ts.js';
|
||||
import { Bot } from '../../bot.ts.js'
|
||||
import { InteractionTypes } from '../../deps.ts.js'
|
||||
import log from '../utils/logger.ts.js'
|
||||
|
||||
Bot.events.interactionCreate = (_, interaction) => {
|
||||
if (!interaction.data) return;
|
||||
if (!interaction.data) return
|
||||
|
||||
switch (interaction.type) {
|
||||
case InteractionTypes.ApplicationCommand:
|
||||
log.info(
|
||||
`[Application Command] ${interaction.data.name} command executed.`,
|
||||
);
|
||||
Bot.commands.get(interaction.data.name!)?.execute(Bot, interaction);
|
||||
break;
|
||||
log.info(`[Application Command] ${interaction.data.name} command executed.`)
|
||||
Bot.commands.get(interaction.data.name!)?.execute(Bot, interaction)
|
||||
break
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import { Bot } from '../../bot.ts.js';
|
||||
import log from '../utils/logger.ts.js';
|
||||
import { Bot } from '../../bot.ts.js'
|
||||
import log from '../utils/logger.ts.js'
|
||||
|
||||
Bot.events.ready = (_, payload) => {
|
||||
log.info(`[READY] Shard ID ${payload.shardId} of ${Bot.gateway.lastShardId + 1} shards is ready!`);
|
||||
log.info(`[READY] Shard ID ${payload.shardId} of ${Bot.gateway.lastShardId + 1} shards is ready!`)
|
||||
|
||||
if (payload.shardId === Bot.gateway.lastShardId) {
|
||||
botFullyReady();
|
||||
botFullyReady()
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// This function lets you run custom code when all your bot's shards are online.
|
||||
function botFullyReady() {
|
||||
// DO STUFF YOU WANT HERE ONCE BOT IS FULLY ONLINE.
|
||||
log.info("[READY] Bot is fully online.");
|
||||
log.info('[READY] Bot is fully online.')
|
||||
}
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
import type { BotClient } from '../../bot.ts.js';
|
||||
import type { ApplicationCommandOption, ApplicationCommandTypes, Interaction } from '../../deps.ts.js';
|
||||
import type { BotClient } from '../../bot.ts.js'
|
||||
import type { ApplicationCommandOption, ApplicationCommandTypes, Interaction } from '../../deps.ts.js'
|
||||
|
||||
export interface Command {
|
||||
/** The name of this command. */
|
||||
name: string;
|
||||
name: string
|
||||
/** What does this command do? */
|
||||
description: string;
|
||||
description: string
|
||||
/** The type of command this is. */
|
||||
type: ApplicationCommandTypes;
|
||||
type: ApplicationCommandTypes
|
||||
/** Whether or not this command is for the dev server only. */
|
||||
devOnly?: boolean;
|
||||
devOnly?: boolean
|
||||
/** The options for this command */
|
||||
options?: ApplicationCommandOption[];
|
||||
options?: ApplicationCommandOption[]
|
||||
/** This will be executed when the command is run. */
|
||||
execute: (bot: BotClient, interaction: Interaction) => unknown;
|
||||
execute: (bot: BotClient, interaction: Interaction) => unknown
|
||||
}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
// This file will export all of the types in this directory.
|
||||
|
||||
export * from './commands.ts.js';
|
||||
export * from './commands.ts.js'
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
export function snowflakeToTimestamp(id: bigint) {
|
||||
return Number(id / 4194304n + 1420070400000n);
|
||||
return Number(id / 4194304n + 1420070400000n)
|
||||
}
|
||||
|
||||
@@ -1,47 +1,40 @@
|
||||
import log from './logger.ts.js';
|
||||
import log from './logger.ts.js'
|
||||
|
||||
// Very important to make sure files are reloaded properly
|
||||
let uniqueFilePathCounter = 0;
|
||||
let paths: string[] = [];
|
||||
let uniqueFilePathCounter = 0
|
||||
let paths: string[] = []
|
||||
|
||||
/** This function allows reading all files in a folder. Useful for loading/reloading commands, monitors etc */
|
||||
export async function importDirectory(path: string) {
|
||||
path = path.replaceAll("\\", "/");
|
||||
const files = Deno.readDirSync(Deno.realPathSync(path));
|
||||
const folder = path.substring(path.indexOf("/src/") + 5);
|
||||
path = path.replaceAll('\\', '/')
|
||||
const files = Deno.readDirSync(Deno.realPathSync(path))
|
||||
const folder = path.substring(path.indexOf('/src/') + 5)
|
||||
|
||||
if (!folder.includes("/")) log.info(`Loading ${folder}...`);
|
||||
if (!folder.includes('/')) log.info(`Loading ${folder}...`)
|
||||
|
||||
for (const file of files) {
|
||||
if (!file.name) continue;
|
||||
if (!file.name) continue
|
||||
|
||||
const currentPath = `${path}/${file.name}`;
|
||||
const currentPath = `${path}/${file.name}`
|
||||
if (file.isFile) {
|
||||
if (!currentPath.endsWith(".ts")) continue;
|
||||
if (!currentPath.endsWith('.ts')) continue
|
||||
paths.push(
|
||||
`import "${Deno.mainModule.substring(0, Deno.mainModule.lastIndexOf("/"))}/${
|
||||
currentPath.substring(
|
||||
currentPath.indexOf("src/"),
|
||||
)
|
||||
}#${uniqueFilePathCounter}";`,
|
||||
);
|
||||
continue;
|
||||
`import "${Deno.mainModule.substring(0, Deno.mainModule.lastIndexOf('/'))}/${currentPath.substring(
|
||||
currentPath.indexOf('src/'),
|
||||
)}#${uniqueFilePathCounter}";`,
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
await importDirectory(currentPath);
|
||||
await importDirectory(currentPath)
|
||||
}
|
||||
|
||||
uniqueFilePathCounter++;
|
||||
uniqueFilePathCounter++
|
||||
}
|
||||
|
||||
/** Imports all everything in fileloader.ts */
|
||||
export async function fileLoader() {
|
||||
await Deno.writeTextFile(
|
||||
"fileloader.ts",
|
||||
paths.join("\n").replaceAll("\\", "/"),
|
||||
);
|
||||
await import(
|
||||
`${Deno.mainModule.substring(0, Deno.mainModule.lastIndexOf("/"))}/fileloader.ts#${uniqueFilePathCounter}`
|
||||
);
|
||||
paths = [];
|
||||
await Deno.writeTextFile('fileloader.ts', paths.join('\n').replaceAll('\\', '/'))
|
||||
await import(`${Deno.mainModule.substring(0, Deno.mainModule.lastIndexOf('/'))}/fileloader.ts#${uniqueFilePathCounter}`)
|
||||
paths = []
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// deno-lint-ignore-file no-explicit-any
|
||||
import { bold, cyan, gray, italic, red, yellow } from '../../deps.ts.js';
|
||||
import { bold, cyan, gray, italic, red, yellow } from '../../deps.ts.js'
|
||||
|
||||
export enum LogLevels {
|
||||
Debug,
|
||||
@@ -10,81 +10,81 @@ export enum LogLevels {
|
||||
}
|
||||
|
||||
const prefixes = new Map<LogLevels, string>([
|
||||
[LogLevels.Debug, "DEBUG"],
|
||||
[LogLevels.Info, "INFO"],
|
||||
[LogLevels.Warn, "WARN"],
|
||||
[LogLevels.Error, "ERROR"],
|
||||
[LogLevels.Fatal, "FATAL"],
|
||||
]);
|
||||
[LogLevels.Debug, 'DEBUG'],
|
||||
[LogLevels.Info, 'INFO'],
|
||||
[LogLevels.Warn, 'WARN'],
|
||||
[LogLevels.Error, 'ERROR'],
|
||||
[LogLevels.Fatal, 'FATAL'],
|
||||
])
|
||||
|
||||
const noColor: (str: string) => string = (msg) => msg;
|
||||
const noColor: (str: string) => string = (msg) => msg
|
||||
const colorFunctions = new Map<LogLevels, (str: string) => string>([
|
||||
[LogLevels.Debug, gray],
|
||||
[LogLevels.Info, cyan],
|
||||
[LogLevels.Warn, yellow],
|
||||
[LogLevels.Error, (str: string) => red(str)],
|
||||
[LogLevels.Fatal, (str: string) => red(bold(italic(str)))],
|
||||
]);
|
||||
])
|
||||
|
||||
export function logger({
|
||||
logLevel = LogLevels.Info,
|
||||
name,
|
||||
}: {
|
||||
logLevel?: LogLevels;
|
||||
name?: string;
|
||||
logLevel?: LogLevels
|
||||
name?: string
|
||||
} = {}) {
|
||||
function log(level: LogLevels, ...args: any[]) {
|
||||
if (level < logLevel) return;
|
||||
if (level < logLevel) return
|
||||
|
||||
let color = colorFunctions.get(level);
|
||||
if (!color) color = noColor;
|
||||
let color = colorFunctions.get(level)
|
||||
if (!color) color = noColor
|
||||
|
||||
const date = new Date();
|
||||
const date = new Date()
|
||||
const log = [
|
||||
`[${date.toLocaleDateString()} ${date.toLocaleTimeString()}]`,
|
||||
color(prefixes.get(level) || "DEBUG"),
|
||||
name ? `${name} >` : ">",
|
||||
color(prefixes.get(level) || 'DEBUG'),
|
||||
name ? `${name} >` : '>',
|
||||
...args,
|
||||
];
|
||||
]
|
||||
|
||||
switch (level) {
|
||||
case LogLevels.Debug:
|
||||
return console.debug(...log);
|
||||
return console.debug(...log)
|
||||
case LogLevels.Info:
|
||||
return console.info(...log);
|
||||
return console.info(...log)
|
||||
case LogLevels.Warn:
|
||||
return console.warn(...log);
|
||||
return console.warn(...log)
|
||||
case LogLevels.Error:
|
||||
return console.error(...log);
|
||||
return console.error(...log)
|
||||
case LogLevels.Fatal:
|
||||
return console.error(...log);
|
||||
return console.error(...log)
|
||||
default:
|
||||
return console.log(...log);
|
||||
return console.log(...log)
|
||||
}
|
||||
}
|
||||
|
||||
function setLevel(level: LogLevels) {
|
||||
logLevel = level;
|
||||
logLevel = level
|
||||
}
|
||||
|
||||
function debug(...args: any[]) {
|
||||
log(LogLevels.Debug, ...args);
|
||||
log(LogLevels.Debug, ...args)
|
||||
}
|
||||
|
||||
function info(...args: any[]) {
|
||||
log(LogLevels.Info, ...args);
|
||||
log(LogLevels.Info, ...args)
|
||||
}
|
||||
|
||||
function warn(...args: any[]) {
|
||||
log(LogLevels.Warn, ...args);
|
||||
log(LogLevels.Warn, ...args)
|
||||
}
|
||||
|
||||
function error(...args: any[]) {
|
||||
log(LogLevels.Error, ...args);
|
||||
log(LogLevels.Error, ...args)
|
||||
}
|
||||
|
||||
function fatal(...args: any[]) {
|
||||
log(LogLevels.Fatal, ...args);
|
||||
log(LogLevels.Fatal, ...args)
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -95,8 +95,8 @@ export function logger({
|
||||
warn,
|
||||
error,
|
||||
fatal,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export const log = logger({ name: "Main" });
|
||||
export default log;
|
||||
export const log = logger({ name: 'Main' })
|
||||
export default log
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Bot } from '../../bot.ts.js';
|
||||
import { configs } from '../../configs.ts.js';
|
||||
import { Bot } from '../../bot.ts.js'
|
||||
import { configs } from '../../configs.ts.js'
|
||||
|
||||
export async function updateApplicationCommands() {
|
||||
await Bot.helpers.upsertGlobalApplicationCommands(
|
||||
@@ -7,7 +7,7 @@ export async function updateApplicationCommands() {
|
||||
// ONLY GLOBAL COMMANDS
|
||||
.filter((command) => !command.devOnly)
|
||||
.array(),
|
||||
);
|
||||
)
|
||||
|
||||
await Bot.helpers.upsertGuildApplicationCommands(
|
||||
configs.devGuildId,
|
||||
@@ -15,5 +15,5 @@ export async function updateApplicationCommands() {
|
||||
// ONLY GLOBAL COMMANDS
|
||||
.filter((command) => !!command.devOnly)
|
||||
.array(),
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user