initial commit
This commit is contained in:
25
init/load-commands.ts
Normal file
25
init/load-commands.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import path from 'path';
|
||||
import {readdirSync} from 'fs';
|
||||
|
||||
import {ExtendedClient} from '../classes/extendedclient';
|
||||
import {logger} from '../logger';
|
||||
|
||||
export async function loadCommands(client: ExtendedClient): Promise<Array<string>>{
|
||||
const foldersPath = path.join(__dirname, '../commands');
|
||||
const commandFolders = readdirSync(foldersPath);
|
||||
const commands: Array<string> = [];
|
||||
for(const folder of commandFolders){
|
||||
const commandsPath = path.join(foldersPath, folder);
|
||||
const commandsFiles = readdirSync(commandsPath).filter(file => file.endsWith('.ts'));
|
||||
for(const file of commandsFiles){
|
||||
const filePath = path.join(commandsPath, file);
|
||||
const data = await import(filePath);
|
||||
if(data.command !== undefined){
|
||||
client.commands.set(data.command.name, data.command);
|
||||
commands.push(data.command.build().toJSON());
|
||||
}else
|
||||
logger.warning(`The command at ${filePath} is missing required properties.`);
|
||||
}
|
||||
}
|
||||
return commands;
|
||||
}
|
||||
11
init/ready-dm.ts
Normal file
11
init/ready-dm.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import {logger} from '../logger';
|
||||
import {ExtendedClient} from '../classes/extendedclient';
|
||||
|
||||
export async function sendReadyDM(client: ExtendedClient, adminId: string): Promise<void>{
|
||||
try{
|
||||
await (await client.users.fetch(adminId)).send(`service up at ${new Date()}`);
|
||||
logger.log('Service up message sent');
|
||||
}catch(err: unknown){
|
||||
logger.warning('sendReadyDM failed.');
|
||||
}
|
||||
}
|
||||
26
init/register-commands.ts
Normal file
26
init/register-commands.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import {REST, Routes} from 'discord.js';
|
||||
|
||||
import {config} from '../config';
|
||||
import {logger} from '../logger';
|
||||
|
||||
function isArray<T>(data: unknown): data is Array<T>{
|
||||
return (data as Array<T>).length !== undefined;
|
||||
}
|
||||
|
||||
export async function registerCommands(commands: Array<string>): Promise<number>{
|
||||
const rest = new REST().setToken(config.token);
|
||||
try{
|
||||
const data = await rest.put(
|
||||
Routes.applicationCommands(config.clientId),
|
||||
{body: commands},
|
||||
);
|
||||
if(!isArray(data)) throw Error('Type error');
|
||||
return data.length;
|
||||
}catch(err: unknown){
|
||||
let message;
|
||||
if(err instanceof Error) message = err.message;
|
||||
else message = String(message);
|
||||
logger.error(`While executing "registerCommands", ${message}`);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
22
init/set-nickname.ts
Normal file
22
init/set-nickname.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import {Guild} from 'discord.js';
|
||||
|
||||
import {ExtendedClient} from '../classes/extendedclient';
|
||||
import {config} from '../config';
|
||||
import {logger} from '../logger';
|
||||
|
||||
export async function setNickname(client: ExtendedClient): Promise<void>{
|
||||
await client.guilds.cache.forEach(async (guild: Guild): Promise<void> => {
|
||||
try{
|
||||
// console.log(guild.members);
|
||||
const self = await guild.members.fetch({user: config.clientId, force: true});
|
||||
if(!self) throw Error('self not exist');
|
||||
await self.setNickname(config.nickname);
|
||||
logger.log(`Nickname had changed in guild: ${guild.name}`);
|
||||
}catch(err: unknown){
|
||||
let message;
|
||||
if(err instanceof Error) message = err.message;
|
||||
else message = String(message);
|
||||
logger.error(`While executing setNickname, ${message}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user