Base structure

This commit is contained in:
2022-11-23 13:23:25 +01:00
parent c7a66fddca
commit 261627570d
11 changed files with 870 additions and 0 deletions

18
src/client/index.ts Normal file
View File

@ -0,0 +1,18 @@
import { Client, GatewayIntentBits } from "discord.js";
const client = new Client({
intents: [
GatewayIntentBits.Guilds
]
});
const token = process.env["TOKEN"];
if (!token) throw new ReferenceError("TOKEN environment variable is missing");
client.login(token);
client.on("ready", () => {
console.log(`Connected to Discord API. Bot account is ${client.user?.tag} (${client.user?.id})`);
});
export default client;

0
src/commands/index.ts Normal file
View File

View File

@ -0,0 +1,51 @@
import { SlashCommandBuilder, PermissionFlagsBits, ChannelType, ChatInputCommandInteraction, NewsChannel, TextBasedChannel, CategoryChannel, StageChannel, TextChannel, PrivateThreadChannel, PublicThreadChannel, VoiceChannel, APIInteractionDataResolvedChannel, ForumChannel } from "discord.js";
import { getSuccessEmbed } from "../tools/defaultEmbeds";
const builder = new SlashCommandBuilder();
builder.setName("logchannel");
builder.setDescription("Configures the log channel");
builder.setDMPermission(false);
builder.setDefaultMemberPermissions(PermissionFlagsBits.ManageChannels);
builder.addChannelOption((option) => {
option.addChannelTypes(ChannelType.GuildText, ChannelType.GuildAnnouncement);
option.setName("Channel");
option.setDescription("The channel to send notifications to");
option.setRequired(true);
return option;
});
const execute = (interaction: ChatInputCommandInteraction) => {
const channel:TextBasedChannel = getTextBasedChannel(interaction.options.getChannel("Channel", true));
//TODO Save channel in config
const embed = getSuccessEmbed();
embed.setDescription(`Log channel has been set to <#${channel.id}>`);
interaction.reply({
embeds: [embed],
ephemeral: true
});
const logMessage = getSuccessEmbed();
logMessage.setDescription("This channel has been selected as the log channel");
logMessage.addFields({
name: "This action was performed by",
value: `${interaction.user.tag} (${interaction.user.id})`
});
channel.send({
embeds: [logMessage]
});
}
function getTextBasedChannel(channel: CategoryChannel | NewsChannel | StageChannel | TextChannel | PrivateThreadChannel | PublicThreadChannel<boolean> | VoiceChannel | ForumChannel | APIInteractionDataResolvedChannel): TextBasedChannel {
if (channel.type === ChannelType.DM || channel.type === ChannelType.GroupDM || channel.type === ChannelType.GuildAnnouncement || channel.type === ChannelType.GuildText || channel.type === ChannelType.PublicThread || channel.type === ChannelType.PrivateThread || channel.type === ChannelType.GuildVoice) {
return channel as TextBasedChannel;
}
throw new TypeError("Channel is not a text based channel");
}
export {
builder,
execute
}

View File

@ -0,0 +1,62 @@
/* eslint-disable prefer-rest-params */
/**
* This module swaps the default console outputs to own functions and adds a timestamp to each message
*/
import path from "path";
import fs from "fs-extra";
export default function() {
fs.ensureDirSync(path.join(__dirname, "../data/log/"));
const callDate = new Date();
const logFileName = `${callDate.getUTCFullYear()}-${`0${callDate.getUTCMonth() + 1}`.slice(-2)}-${`0${callDate.getUTCDate()}`.slice(-2)}-${`0${callDate.getUTCHours()}`.slice(-2)}-${`0${callDate.getUTCMinutes()}`.slice(-2)}-${`0${callDate.getUTCSeconds()}`.slice(-2)}-${`00${callDate.getUTCMilliseconds()}`.slice(-3)}.log`;
const logFile = fs.createWriteStream(path.join(__dirname, `../data/log/${logFileName}`));
const out = new console.Console(process.stdout, process.stderr, true);
const logConsole = new console.Console(logFile);
function log() {
const now = new Date();
const Prepend = `[i] [${now.getUTCFullYear()}-${`0${now.getUTCMonth() + 1}`.slice(-2)}-${`0${now.getUTCDate()}`.slice(-2)} ${`0${now.getUTCHours()}`.slice(-2)}:${`0${now.getUTCMinutes()}`.slice(-2)}:${`0${now.getUTCSeconds()}`.slice(-2)}.${`00${now.getUTCMilliseconds()}`.slice(-3)}] `;
arguments[0] = `${Prepend}${arguments[0].toString().normalize()}`;
for (let i = 0; i < arguments.length; i++) {
arguments[i] = arguments[i].split("\n").join(`\n${new Array(31).join(" ")}`);
}
out.log(...arguments);
logConsole.log(...arguments);
}
function warn() {
const now = new Date();
const Prepend = `[W] [${now.getUTCFullYear()}-${`0${now.getUTCMonth() + 1}`.slice(-2)}-${`0${now.getUTCDate()}`.slice(-2)} ${`0${now.getUTCHours()}`.slice(-2)}:${`0${now.getUTCMinutes()}`.slice(-2)}:${`0${now.getUTCSeconds()}`.slice(-2)}.${`00${now.getUTCMilliseconds()}`.slice(-3)}] `;
arguments[0] = `${Prepend}${arguments[0].toString().normalize()}`;
for (let i = 0; i < arguments.length; i++) {
const argument = arguments[i].toString().normalize();
arguments[i] = argument.split("\n").join(`\n${new Array(31).join(" ")}`);
}
out.warn(...arguments);
logConsole.warn(...arguments);
}
function error() {
const now = new Date();
const Prepend = `==== [ERROR] [${now.getUTCFullYear()}-${`0${now.getUTCMonth() + 1}`.slice(-2)}-${`0${now.getUTCDate()}`.slice(-2)} ${`0${now.getUTCHours()}`.slice(-2)}:${`0${now.getUTCMinutes()}`.slice(-2)}:${`0${now.getUTCSeconds()}`.slice(-2)}.${`00${now.getUTCMilliseconds()}`.slice(-3)}] ====\n`;
arguments[0] = `${Prepend}${arguments[0].toString().normalize()}`;
for (let i = 0; i < arguments.length; i++) {
arguments[i] = arguments[i].toString().normalize();
}
out.error(...arguments);
logConsole.error(...arguments);
}
console.log = log;
console.warn = warn;
console.error = error;
}

View File

@ -0,0 +1,24 @@
import pack from "../../package.json";
import { EmbedBuilder } from "discord.js";
import client from "../client";
// const _coolColors = [0x054566];
export default function getDefaultEmbed(): EmbedBuilder {
const embed = new EmbedBuilder();
embed.setFooter({
text: `Channel filter V${pack.version} by AstroGD®`,
iconURL: client.user?.avatarURL() || undefined,
});
embed.setTimestamp(new Date());
embed.setColor(0x3682cc);
return embed;
}
export function getSuccessEmbed(): EmbedBuilder {
const embed = getDefaultEmbed();
embed.setTitle("Success");
embed.setColor(0x32d122);
return embed;
}