From d7987b87ae170c07cbfbda8345f5139bb0e9b0c1 Mon Sep 17 00:00:00 2001 From: EmotionChild Date: Thu, 27 Jan 2022 23:36:38 +1300 Subject: [PATCH] Made the central system --- .idea/.gitignore | 5 +++ .idea/Ellie-v4.iml | 12 ++++++ .idea/discord.xml | 7 ++++ .idea/modules.xml | 8 ++++ .idea/vcs.xml | 6 +++ CHANGELOG.md | 12 +++++- src/client/client.ts | 9 +++-- src/commands/mod/BanCommand.ts | 37 +++++++++++++++++++ src/commands/mod/ChprefixCommand.ts | 35 ++++++++++++++++++ src/commands/mod/ChwelcomechannelCommand.ts | 35 ++++++++++++++++++ src/commands/mod/KickCommand.ts | 38 +++++++++++++++++++ src/commands/mod/TimeoutCommand.ts | 41 +++++++++++++++++++++ src/events/GuildMemberAddEvent.ts | 27 ++++++++++++++ src/events/message/MessageEvent.ts | 13 +++++-- src/index.ts | 2 +- src/typeorm/entities/GuildBanLog.ts | 22 +++++++++++ src/typeorm/entities/ModerationLog.ts | 26 +++++++++++++ src/typeorm/entities/index.ts | 5 +++ src/utils/types.ts | 1 + 19 files changed, 331 insertions(+), 10 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/Ellie-v4.iml create mode 100644 .idea/discord.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 src/commands/mod/BanCommand.ts create mode 100644 src/commands/mod/ChprefixCommand.ts create mode 100644 src/commands/mod/ChwelcomechannelCommand.ts create mode 100644 src/commands/mod/KickCommand.ts create mode 100644 src/commands/mod/TimeoutCommand.ts create mode 100644 src/typeorm/entities/GuildBanLog.ts create mode 100644 src/typeorm/entities/ModerationLog.ts create mode 100644 src/typeorm/entities/index.ts create mode 100644 src/utils/types.ts diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..b58b603 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,5 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/Ellie-v4.iml b/.idea/Ellie-v4.iml new file mode 100644 index 0000000..0c8867d --- /dev/null +++ b/.idea/Ellie-v4.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/discord.xml b/.idea/discord.xml new file mode 100644 index 0000000..d8e9561 --- /dev/null +++ b/.idea/discord.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..d7dbcb8 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index ef43b92..99bbe64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,12 +2,20 @@ Experimental changelog. Mostly based on [keepachangelog](https://keepachangelog.com/en/1.0.0/) except date format. a-c-f-r-o -## [4.0.6-alpha] - 16-01.2022 +##[4.0.6-beta1] - 27-01-2022 + +### Added + +- Most of the main base system + +## [4.0.6-alpha] - 16-01-2022 + +### Added -## Added - Added the groundwork for the database system ## [4.0.5-alpha] - 6-01-2022 ### Added + - Initial bot system stuff \ No newline at end of file diff --git a/src/client/client.ts b/src/client/client.ts index fb75eb9..5b389ce 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -4,7 +4,6 @@ import BaseCommand from '../utils/structures/BaseCommand'; import { GuildConfiguration } from '../typeorm/entities/GuildConfiguration'; export default class DiscordClient extends Client { - private _commands = new Collection(); private _events = new Collection(); private _prefix: string = '!'; @@ -23,9 +22,13 @@ export default class DiscordClient extends Client { get prefix(): string { return this._prefix; } - set prefix(prefix: string) { this._prefix = prefix; } - + get configs() { + return this._configs; + } + set configs(guildConfigs: Collection) { + this._configs = guildConfigs; + } } diff --git a/src/commands/mod/BanCommand.ts b/src/commands/mod/BanCommand.ts new file mode 100644 index 0000000..b64058b --- /dev/null +++ b/src/commands/mod/BanCommand.ts @@ -0,0 +1,37 @@ +import { Message } from "discord.js"; +import BaseCommand from "../../utils/structures/BaseCommand"; +import DiscordClient from "../../client/client"; +import { getRepository, Repository } from "typeorm"; +import { GuildBanLog } from '../../typeorm/entities/GuildBanLog'; +import { ModerationLog } from "../../typeorm/entities/ModerationLog"; + +export default class BanCommand extends BaseCommand { + constructor( + private readonly modLogRepository: Repository = getRepository( + ModerationLog + ) + ) { + super('ban', 'mod', []); + } + + async run(client: DiscordClient, message: Message, args: Array) { + console.log(args); + const [memberId, ...rest] = args; + const reason = rest.join(' '); + try { + // const member = await message.guild?.members.fetch(memberId)!; + // await member.ban({ reason }); + const guildBan = this.modLogRepository.create({ + guildId: message.guildId!, + issuedBy: message.author.id, + issuedOn: new Date(), + type: 'ban', + reason, + memberId, + }); + await this.modLogRepository.save(guildBan); + } catch (err) { + console.log(err); + } + } +} \ No newline at end of file diff --git a/src/commands/mod/ChprefixCommand.ts b/src/commands/mod/ChprefixCommand.ts new file mode 100644 index 0000000..04743a0 --- /dev/null +++ b/src/commands/mod/ChprefixCommand.ts @@ -0,0 +1,35 @@ +import { Message } from "discord.js"; +import BaseCommand from "../../utils/structures/BaseCommand"; +import DiscordClient from "../../client/client"; +import { getRepository } from "typeorm"; +import { GuildConfiguration } from "../../typeorm/entities/GuildConfiguration"; + +export default class ChprefixCommand extends BaseCommand { + constructor( + private readonly guildConfigRepository = getRepository(GuildConfiguration) + ) { + super('chprefix', 'mod', []); + } + + async run(client: DiscordClient, message: Message, args: Array) { + if (!args.length) { + message.channel.send('Please provide an argument!'); + return; + } + const [newPrefix] = args; + try { + const config = client.configs.get(message.guildId!); + const updatedConfig = await this.guildConfigRepository.save({ + ...config, + prefix: newPrefix, + }); + console.log(updatedConfig); + message.channel.send('Updated prefix successfully!'); + client.configs.set(message.guildId!, updatedConfig); + console.log(client.configs); + } catch (err) { + console.log(err); + message.channel.send('Something went wrong.'); + } + } +} \ No newline at end of file diff --git a/src/commands/mod/ChwelcomechannelCommand.ts b/src/commands/mod/ChwelcomechannelCommand.ts new file mode 100644 index 0000000..bf201ef --- /dev/null +++ b/src/commands/mod/ChwelcomechannelCommand.ts @@ -0,0 +1,35 @@ +import { Message } from "discord.js"; +import BaseCommand from "../../utils/structures/BaseCommand"; +import DiscordClient from "../../client/client"; +import { GuildConfiguration } from "../../typeorm/entities/GuildConfiguration"; +import { getRepository } from "typeorm"; + +export default class ChwelcomechannelCommand extends BaseCommand { + constructor( + private readonly guildConfigRepository = getRepository(GuildConfiguration) + ) { + super('chwemcomechannel', 'mod', []); + } + + async run(client: DiscordClient, message: Message, args: Array) { + if (!args.length) { + message.channel.send('Please provide an argument!'); + return; + } + const [newChannelId] = args; + try { + const config = client.configs.get(message.guildId!); + const updatedConfig = await this.guildConfigRepository.save({ + ...config, + welcomeChannelId: newChannelId, + }); + console.log(updatedConfig); + message.channel.send('Updated Welcome Channel successfully!'); + client.configs.set(message.guildId!, updatedConfig); + console.log(client.configs); + } catch (err) { + console.log(err); + message.channel.send('Something went wrong.'); + } + } +} \ No newline at end of file diff --git a/src/commands/mod/KickCommand.ts b/src/commands/mod/KickCommand.ts new file mode 100644 index 0000000..92b9a54 --- /dev/null +++ b/src/commands/mod/KickCommand.ts @@ -0,0 +1,38 @@ +import { Message } from "discord.js"; +import BaseCommand from "../../utils/structures/BaseCommand"; +import DiscordClient from "../../client/client"; +import { getRepository, Repository } from "typeorm"; +import { ModerationLog } from "../../typeorm/entities/ModerationLog"; + +export default class KickCommand extends BaseCommand { + constructor( + private readonly modLogRepository: Repository = getRepository( + ModerationLog + ) + ) { + super('kick', 'mod', []); + } + + async run(client: DiscordClient, message: Message, args: Array) { + console.log(args); + const [memberId, ...rest] = args; + const reason = rest.join(' '); + try { + // const member = await message.guild?.members.fetch(memberId)!; + // await member.kick(reason); + const date = new Date(); + date.setDate(date.getDate() - 6); + const modLog = this.modLogRepository.create({ + guildId: message.guildId!, + memberId, + issuedBy: message.author.id, + issuedOn: date, + reason, + type: 'kick' + }); + await this.modLogRepository.save(modLog); + } catch (err) { + console.log(err); + } + } +} \ No newline at end of file diff --git a/src/commands/mod/TimeoutCommand.ts b/src/commands/mod/TimeoutCommand.ts new file mode 100644 index 0000000..2f00425 --- /dev/null +++ b/src/commands/mod/TimeoutCommand.ts @@ -0,0 +1,41 @@ +import { Message } from 'discord.js'; +import BaseCommand from "../../utils/structures/BaseCommand"; +import DiscordClient from "../../client/client"; +import { getRepository, Repository } from "typeorm"; +import { ModerationLog } from "../../typeorm/entities/ModerationLog"; + +export default class TimeoutCommand extends BaseCommand { + constructor( + private readonly modLogRepository: Repository = getRepository( + ModerationLog + ) + ) { + super('timeout', 'mod', []); + } + + async run(client: DiscordClient, message: Message, args: Array) { + console.log(args); + const [memberId, timeoutStr, ...rest] = args; + const reason = rest.join(' '); + const time = parseInt(timeoutStr); + if (isNaN(time)) { + message.channel.send('Invalid Time'); + return; + } + try { + const member = await message.guild?.members.fetch(memberId)!; + await member.timeout(time * 1000, reason); + const modLog = this.modLogRepository.create({ + guildId: message.guildId!, + memberId, + issuedBy: message.author.id, + issuedOn: new Date(), + reason, + type: 'timeout', + }); + await this.modLogRepository.save(modLog); + } catch (err) { + console.log(err); + } + } +} \ No newline at end of file diff --git a/src/events/GuildMemberAddEvent.ts b/src/events/GuildMemberAddEvent.ts index e69de29..1effe4b 100644 --- a/src/events/GuildMemberAddEvent.ts +++ b/src/events/GuildMemberAddEvent.ts @@ -0,0 +1,27 @@ +// https://discord.js.org/#/docs/main/stable/class/Client?scrollTo=e-guildMemberAdd +import { GuildMember, TextChannel} from "discord.js"; +import BaseEvent from "../utils/structures/BaseEvent"; +import DiscordClient from "../client/client"; + +export default class GuildMemberAddEvent extends BaseEvent { + constructor() { + super('guildMemberAdd'); + } + + async run(client: DiscordClient, member: GuildMember) { + console.log(`Guild Member Joined`); + console.log(`Joined ${member.guild.id} ${member.guild.name}`); + const config = client.configs.get(member.guild.id); + console.log(config); + if (!config) return; + if (config.welcomeChannelId) { + const channel = member.guild.channels.cache.get( + config.welcomeChannelId + ) as TextChannel; + if (!channel) console.log(`No welcome channel found`); + else channel.send(`Welcome ${member}`); + } else { + console.log('No welcome channel set.'); + } + } +} \ No newline at end of file diff --git a/src/events/message/MessageEvent.ts b/src/events/message/MessageEvent.ts index 9f8ac4c..7267fd4 100644 --- a/src/events/message/MessageEvent.ts +++ b/src/events/message/MessageEvent.ts @@ -9,11 +9,16 @@ export default class MessageEvent extends BaseEvent { async run(client: DiscordClient, message: Message) { if (message.author.bot) return; - if (message.content.startsWith(client.prefix)) { + const config = client.configs.get(message.guildId!); + if (!config) { + message.channel.send('No configuration set.'); + return; + } + if (message.content.startsWith(config.prefix)) { const [cmdName, ...cmdArgs] = message.content - .slice(client.prefix.length) - .trim() - .split(/\s+/); + .slice(config.prefix.length) + .trim() + .split(/\s+/); const command = client.commands.get(cmdName); if (command) { command.run(client, message, cmdArgs); diff --git a/src/index.ts b/src/index.ts index 3058f77..9d73247 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,7 @@ import DiscordClient from './client/client'; import { Collection, Intents } from 'discord.js'; import { createConnection, getRepository } from 'typeorm'; import { GuildConfiguration } from './typeorm/entities/GuildConfiguration'; -import {io} from 'socket.io-client'; +import { io } from 'socket.io-client'; import { entities } from './typeorm/entities'; const client = new DiscordClient({ diff --git a/src/typeorm/entities/GuildBanLog.ts b/src/typeorm/entities/GuildBanLog.ts new file mode 100644 index 0000000..c350d44 --- /dev/null +++ b/src/typeorm/entities/GuildBanLog.ts @@ -0,0 +1,22 @@ +import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; + +@Entity({ name: 'guild_bans' }) +export class GuildBanLog { + @PrimaryGeneratedColumn() + id: number; + + @Column({ name: 'guild_id' }) + guildId: string; + + @Column({ name: 'banned_member_id' }) + bannedMemberId: string; + + @Column({ name: 'issued_by' }) + issuedBy: string; + + @Column() + reason?: string; + + @Column({ name: 'issued_on' }) + issuedOn: Date; +} \ No newline at end of file diff --git a/src/typeorm/entities/ModerationLog.ts b/src/typeorm/entities/ModerationLog.ts new file mode 100644 index 0000000..cfe01f9 --- /dev/null +++ b/src/typeorm/entities/ModerationLog.ts @@ -0,0 +1,26 @@ +import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; +import { ModerationActionType } from "../../utils/types"; + +@Entity({ name: 'moderation_logs' }) +export class ModerationLog { + @PrimaryGeneratedColumn() + id: number; + + @Column({ name: 'guild_id' }) + guildId: string; + + @Column({ name: 'member_id' }) + memberId: string; + + @Column({ name: 'issued_by' }) + issuedBy: string; + + @Column() + reason?: string; + + @Column({ name: 'issued_on' }) + issuedOn: Date; + + @Column() + type: ModerationActionType; +} \ No newline at end of file diff --git a/src/typeorm/entities/index.ts b/src/typeorm/entities/index.ts new file mode 100644 index 0000000..b776041 --- /dev/null +++ b/src/typeorm/entities/index.ts @@ -0,0 +1,5 @@ +import { GuildBanLog } from "./GuildBanLog"; +import { GuildConfiguration } from "./GuildConfiguration"; +import { ModerationLog } from "./ModerationLog"; + +export const entities = [GuildBanLog, GuildConfiguration, ModerationLog]; \ No newline at end of file diff --git a/src/utils/types.ts b/src/utils/types.ts new file mode 100644 index 0000000..a451744 --- /dev/null +++ b/src/utils/types.ts @@ -0,0 +1 @@ +export type ModerationActionType = 'ban' | 'kick' | 'timeout'; \ No newline at end of file