Base structure #2
This commit is contained in:
@ -15,4 +15,6 @@ client.on("ready", () => {
|
||||
console.log(`Connected to Discord API. Bot account is ${client.user?.tag} (${client.user?.id})`);
|
||||
});
|
||||
|
||||
export default client;
|
||||
export default client;
|
||||
|
||||
import "../commands";
|
7
src/commands/ci.ts
Normal file
7
src/commands/ci.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import * as notification from "./notification";
|
||||
|
||||
const array = [notification.builder.toJSON()];
|
||||
|
||||
export {
|
||||
array
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
import { ChatInputCommandInteraction, Collection, Events, SlashCommandBuilder } from "discord.js";
|
||||
import * as notification from "./notification";
|
||||
import client from "../client";
|
||||
import getDefaultEmbed from "../tools/defaultEmbeds";
|
||||
|
||||
const commands = new Collection<string, { builder: SlashCommandBuilder, execute: (interaction: ChatInputCommandInteraction) => Promise<void> }>();
|
||||
commands.set(notification.builder.name, notification);
|
||||
|
||||
client.on(Events.InteractionCreate, async (interaction) => {
|
||||
if (!interaction.isChatInputCommand()) return;
|
||||
|
||||
const command = commands.get(interaction.commandName);
|
||||
|
||||
if (!command) {
|
||||
console.error(`No command matching ${interaction.commandName} was found`);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await command.execute(interaction);
|
||||
} catch (e) {
|
||||
|
||||
if (e instanceof Error && "stack" in e) {
|
||||
console.error(e.stack);
|
||||
} else {
|
||||
console.error(e)
|
||||
}
|
||||
|
||||
|
||||
const embed = getDefaultEmbed();
|
||||
embed.setTitle("Something went wrong");
|
||||
embed.setDescription("An unexpected error occurred while processing your command. Please contact support if the problem persists");
|
||||
embed.setColor(0xD01B15);
|
||||
await interaction.reply({
|
||||
embeds: [embed],
|
||||
ephemeral: true
|
||||
}).catch();
|
||||
}
|
||||
});
|
@ -1,5 +1,7 @@
|
||||
import { SlashCommandBuilder, PermissionFlagsBits, ChannelType, ChatInputCommandInteraction, NewsChannel, TextBasedChannel, CategoryChannel, StageChannel, TextChannel, PrivateThreadChannel, PublicThreadChannel, VoiceChannel, APIInteractionDataResolvedChannel, ForumChannel } from "discord.js";
|
||||
import { getSuccessEmbed } from "../tools/defaultEmbeds";
|
||||
import { SlashCommandBuilder, PermissionFlagsBits, ChannelType, ChatInputCommandInteraction, NewsChannel, TextBasedChannel, CategoryChannel, StageChannel, TextChannel, PrivateThreadChannel, PublicThreadChannel, VoiceChannel, APIInteractionDataResolvedChannel, ForumChannel, GuildBasedChannel } from "discord.js";
|
||||
import getDefaultEmbed, { getSuccessEmbed } from "../tools/defaultEmbeds";
|
||||
import { database, GuildSetting } from "../data";
|
||||
import client from "../client";
|
||||
|
||||
const builder = new SlashCommandBuilder();
|
||||
builder.setName("logchannel");
|
||||
@ -8,37 +10,119 @@ builder.setDMPermission(false);
|
||||
builder.setDefaultMemberPermissions(PermissionFlagsBits.ManageChannels);
|
||||
builder.addChannelOption((option) => {
|
||||
option.addChannelTypes(ChannelType.GuildText, ChannelType.GuildAnnouncement);
|
||||
option.setName("Channel");
|
||||
option.setName("channel");
|
||||
option.setDescription("The channel to send notifications to");
|
||||
option.setRequired(true);
|
||||
option.setRequired(false);
|
||||
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
|
||||
async function getGuildSetting(guildID: string): Promise<GuildSetting> {
|
||||
let guildSetting = await database.getRepository(GuildSetting).findOne({
|
||||
where: {
|
||||
id: guildID
|
||||
}
|
||||
});
|
||||
|
||||
const logMessage = getSuccessEmbed();
|
||||
logMessage.setDescription("This channel has been selected as the log channel");
|
||||
logMessage.addFields({
|
||||
if (!guildSetting) {
|
||||
guildSetting = new GuildSetting();
|
||||
guildSetting.id = guildID;
|
||||
guildSetting.isPremiumUntil = null;
|
||||
guildSetting.notificationChannelID = null;
|
||||
}
|
||||
|
||||
return guildSetting;
|
||||
}
|
||||
|
||||
async function getGuildChannel(guildID: string, channelID: string): Promise<GuildBasedChannel | null> {
|
||||
const guild = await client.guilds.fetch(guildID);
|
||||
if (!guild) return null;
|
||||
|
||||
const channel = await guild.channels.fetch(channelID);
|
||||
return channel;
|
||||
}
|
||||
|
||||
async function resetNotificationChannel(guildSetting: GuildSetting, interaction: ChatInputCommandInteraction): Promise<void> {
|
||||
const logChannel = guildSetting.notificationChannelID ? await getGuildChannel(guildSetting.id, guildSetting.notificationChannelID) : null;
|
||||
|
||||
guildSetting.notificationChannelID = null;
|
||||
await database.getRepository(GuildSetting).save(guildSetting);
|
||||
|
||||
const logEmbed = getDefaultEmbed();
|
||||
logEmbed.setTitle("Settings changed");
|
||||
logEmbed.setDescription("Log channel has been disabled");
|
||||
logEmbed.addFields({
|
||||
name: "This action was performed by",
|
||||
value: `${interaction.user.tag} (${interaction.user.id})`
|
||||
});
|
||||
channel.send({
|
||||
embeds: [logMessage]
|
||||
|
||||
if (logChannel && logChannel.isTextBased()) {
|
||||
logChannel.send({
|
||||
embeds: [logEmbed]
|
||||
}).catch();
|
||||
}
|
||||
|
||||
const embed = getSuccessEmbed();
|
||||
embed.setDescription("Log channel has been disabled");
|
||||
interaction.reply({
|
||||
embeds: [embed],
|
||||
ephemeral: true
|
||||
}).catch();
|
||||
}
|
||||
|
||||
const execute = async (interaction: ChatInputCommandInteraction) => {
|
||||
if (!interaction.guildId) throw new Error("Command can only be used inside a guild");
|
||||
|
||||
const optionVal = interaction.options.getChannel("channel", false);
|
||||
const guildSetting = await getGuildSetting(interaction.guildId);
|
||||
|
||||
if (!optionVal) return await resetNotificationChannel(guildSetting, interaction);
|
||||
|
||||
const channel = getTextBasedChannel(optionVal);
|
||||
if (guildSetting.notificationChannelID) {
|
||||
const oldLogChannel = await getGuildChannel(guildSetting.id, guildSetting.notificationChannelID);
|
||||
const embed = getDefaultEmbed();
|
||||
embed.setTitle("Settings changed");
|
||||
embed.setDescription(`Log channel has been switched to <#${channel.id}>`);
|
||||
embed.addFields({
|
||||
name: "This action was performed by",
|
||||
value: `${interaction.user.tag} (${interaction.user.id})`
|
||||
});
|
||||
|
||||
if (oldLogChannel && oldLogChannel.isTextBased()) {
|
||||
oldLogChannel.send({
|
||||
embeds: [embed]
|
||||
}).catch();
|
||||
}
|
||||
}
|
||||
|
||||
guildSetting.notificationChannelID = channel.id;
|
||||
await database.getRepository(GuildSetting).save(guildSetting);
|
||||
|
||||
const embed = getDefaultEmbed();
|
||||
embed.setTitle("Settings changed");
|
||||
embed.setDescription("This channel has been set as the log channel");
|
||||
embed.addFields({
|
||||
name: "This action was performed by",
|
||||
value: `${interaction.user.tag} (${interaction.user.id})`
|
||||
});
|
||||
|
||||
channel.send({
|
||||
embeds: [ embed ]
|
||||
}).catch();
|
||||
|
||||
const reply = getSuccessEmbed();
|
||||
reply.setDescription(`Log channel was set to <#${channel.id}>`);
|
||||
|
||||
interaction.reply({
|
||||
embeds: [ reply ],
|
||||
ephemeral: true
|
||||
}).catch();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
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) {
|
||||
if (channel.type === ChannelType.GuildAnnouncement || channel.type === ChannelType.GuildText || channel.type === ChannelType.PublicThread || channel.type === ChannelType.PrivateThread || channel.type === ChannelType.GuildVoice) {
|
||||
return channel as TextBasedChannel;
|
||||
}
|
||||
|
||||
|
31
src/data/dataSource.ts
Normal file
31
src/data/dataSource.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import path from "path";
|
||||
import { DataSource } from "typeorm";
|
||||
import { config } from "dotenv";
|
||||
import { Badword, GuildSetting } from "./model";
|
||||
|
||||
config();
|
||||
|
||||
const host = process.env["DB_HOST"];
|
||||
const username = process.env["DB_USERNAME"];
|
||||
const password = process.env["DB_PASSWORD"];
|
||||
const database = process.env["DB_DATABASE"];
|
||||
|
||||
if (!host) throw new ReferenceError("Environment variable DB_HOST is missing");
|
||||
if (!username) throw new ReferenceError("Environment variable DB_USERNAME is missing");
|
||||
if (!password) throw new ReferenceError("Environment variable DB_PASSWORD is missing");
|
||||
if (!database) throw new ReferenceError("Environment variable DB_DATABASE is missing");
|
||||
|
||||
const dataSource = new DataSource({
|
||||
type: "postgres",
|
||||
host: host,
|
||||
port: 5432,
|
||||
username: username,
|
||||
password: password,
|
||||
database: database,
|
||||
migrationsRun: true,
|
||||
migrations: [ path.join(__dirname, "/migrations/*") ],
|
||||
entities: [Badword,GuildSetting],
|
||||
migrationsTransactionMode: "each"
|
||||
});
|
||||
|
||||
export default dataSource;
|
9
src/data/index.ts
Normal file
9
src/data/index.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import dataSource from "./dataSource";
|
||||
import { Badword, GuildSetting } from "./model";
|
||||
|
||||
export {
|
||||
dataSource as database,
|
||||
Badword,
|
||||
GuildSetting
|
||||
}
|
||||
|
34
src/data/migrations/1669251793386-data.ts
Normal file
34
src/data/migrations/1669251793386-data.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import { MigrationInterface, QueryRunner } from "typeorm";
|
||||
|
||||
export class data1669251793386 implements MigrationInterface {
|
||||
name = 'data1669251793386'
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`
|
||||
CREATE TABLE "badword" (
|
||||
"id" uuid NOT NULL DEFAULT uuid_generate_v4(),
|
||||
"guildID" character varying,
|
||||
"value" character varying NOT NULL,
|
||||
CONSTRAINT "PK_b5034b5fcec4ccac0c288e37f3a" PRIMARY KEY ("id")
|
||||
)
|
||||
`);
|
||||
await queryRunner.query(`
|
||||
CREATE TABLE "guild_setting" (
|
||||
"id" character varying NOT NULL,
|
||||
"notificationChannelID" character varying,
|
||||
"isPremiumUntil" date,
|
||||
CONSTRAINT "PK_56f0d706a92e999b4e967abae5f" PRIMARY KEY ("id")
|
||||
)
|
||||
`);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`
|
||||
DROP TABLE "guild_setting"
|
||||
`);
|
||||
await queryRunner.query(`
|
||||
DROP TABLE "badword"
|
||||
`);
|
||||
}
|
||||
|
||||
}
|
13
src/data/model/badword.ts
Normal file
13
src/data/model/badword.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { Entity, PrimaryGeneratedColumn, Column } from "typeorm";
|
||||
|
||||
@Entity()
|
||||
export class Badword {
|
||||
@PrimaryGeneratedColumn("uuid")
|
||||
id!: string;
|
||||
|
||||
@Column("varchar", { nullable: true })
|
||||
guildID!: string | null;
|
||||
|
||||
@Column("varchar")
|
||||
value!: string;
|
||||
}
|
13
src/data/model/guildSetting.ts
Normal file
13
src/data/model/guildSetting.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { Entity, PrimaryColumn, Column } from "typeorm";
|
||||
|
||||
@Entity()
|
||||
export class GuildSetting {
|
||||
@PrimaryColumn("varchar")
|
||||
id!: string;
|
||||
|
||||
@Column("varchar", { nullable: true, default: null })
|
||||
notificationChannelID!: string | null;
|
||||
|
||||
@Column("date", { nullable: true, default: null })
|
||||
isPremiumUntil!: Date | null;
|
||||
}
|
7
src/data/model/index.ts
Normal file
7
src/data/model/index.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { Badword } from "./badword";
|
||||
import { GuildSetting } from "./guildSetting";
|
||||
|
||||
export {
|
||||
Badword,
|
||||
GuildSetting
|
||||
}
|
@ -1,24 +1,25 @@
|
||||
/* eslint-disable prefer-rest-params */
|
||||
/**
|
||||
* This module swaps the default console outputs to own functions and adds a timestamp to each message
|
||||
* (c) 2022 AstroGD
|
||||
*/
|
||||
|
||||
import path from "path";
|
||||
import fs from "fs-extra";
|
||||
|
||||
export default function() {
|
||||
fs.ensureDirSync(path.join(__dirname, "../data/log/"));
|
||||
fs.ensureDirSync(path.join(__dirname, "../../data/logs/"));
|
||||
|
||||
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 logFile = fs.createWriteStream(path.join(__dirname, `../../data/logs/${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()}`;
|
||||
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(" ")}`);
|
||||
@ -31,10 +32,10 @@
|
||||
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()}`;
|
||||
arguments[0] = `${Prepend}${arguments[0]?.toString().normalize()}`;
|
||||
|
||||
for (let i = 0; i < arguments.length; i++) {
|
||||
const argument = arguments[i].toString().normalize();
|
||||
const argument = arguments[i]?.toString().normalize();
|
||||
arguments[i] = argument.split("\n").join(`\n${new Array(31).join(" ")}`);
|
||||
}
|
||||
|
||||
@ -45,10 +46,10 @@
|
||||
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()}`;
|
||||
arguments[0] = `${Prepend}${arguments[0]?.toString().normalize()}`;
|
||||
|
||||
for (let i = 0; i < arguments.length; i++) {
|
||||
arguments[i] = arguments[i].toString().normalize();
|
||||
arguments[i] = arguments[i]?.toString().normalize();
|
||||
}
|
||||
|
||||
out.error(...arguments);
|
||||
|
Reference in New Issue
Block a user