Compare commits
5 Commits
1.0.0-alph
...
1.0.0-alph
Author | SHA1 | Date | |
---|---|---|---|
cd60d7dabc
|
|||
62aa05031f
|
|||
dc6755eb19
|
|||
def44d2774
|
|||
a26fb2df12
|
13
README.md
13
README.md
@ -5,6 +5,19 @@ A Discord bot that checks Discord channel names for banned words and prevents re
|
||||
### /logchanel [channel?]
|
||||
Sets the channel where the bot will log if a channel meets the banned word criteria. If channel is omitted, the log channel will be disabled.
|
||||
|
||||
### /blocklist
|
||||
#### /blocklist get
|
||||
Gets the global and server specific banned word list and returns it
|
||||
|
||||
#### /blocklist add [word]
|
||||
Adds the word to the server specific blocklist
|
||||
|
||||
#### /blocklist remove [word]
|
||||
Removes the word from the server specific blocklist
|
||||
|
||||
### /info
|
||||
Returns general information about the bot and the servers stats
|
||||
|
||||
## Environment variables
|
||||
| Name | Description | Required | Example |
|
||||
| :---------- | :------------------------------------------------------------ | :------: | :------------------ |
|
||||
|
@ -1,8 +1,10 @@
|
||||
version: "2"
|
||||
version: "3.9"
|
||||
services:
|
||||
app:
|
||||
image: astrogd/white-leopard:dev
|
||||
build: ./
|
||||
tty: true
|
||||
stdin_open: true
|
||||
depends_on:
|
||||
- database
|
||||
restart: unless-stopped
|
||||
|
3
index.ts
3
index.ts
@ -6,6 +6,8 @@ swapConsole();
|
||||
|
||||
import "./src/client/init";
|
||||
import "./src/commands";
|
||||
import "./src/events";
|
||||
import "./src/cli";
|
||||
import client from "./src/client";
|
||||
|
||||
function shutdown() {
|
||||
@ -16,3 +18,4 @@ function shutdown() {
|
||||
|
||||
process.on("SIGINT", shutdown);
|
||||
process.on("SIGHUP", shutdown);
|
||||
process.on("SIGTERM", shutdown);
|
13
package-lock.json
generated
13
package-lock.json
generated
@ -1,17 +1,18 @@
|
||||
{
|
||||
"name": "eu.astrogd.white-leopard",
|
||||
"version": "1.0.0-alpha.1",
|
||||
"version": "1.0.0-alpha.5",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "eu.astrogd.white-leopard",
|
||||
"version": "1.0.0-alpha.1",
|
||||
"version": "1.0.0-alpha.5",
|
||||
"license": "CC-BY-NC-ND-4.0",
|
||||
"dependencies": {
|
||||
"discord.js": "^14.6.0",
|
||||
"dotenv": "^16.0.3",
|
||||
"fs-extra": "^10.1.0",
|
||||
"moment": "^2.29.4",
|
||||
"pg": "^8.8.0",
|
||||
"typeorm": "^0.3.10"
|
||||
},
|
||||
@ -771,6 +772,14 @@
|
||||
"node": ">=10"
|
||||
}
|
||||
},
|
||||
"node_modules/moment": {
|
||||
"version": "2.29.4",
|
||||
"resolved": "https://registry.npmjs.org/moment/-/moment-2.29.4.tgz",
|
||||
"integrity": "sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==",
|
||||
"engines": {
|
||||
"node": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/ms": {
|
||||
"version": "2.1.2",
|
||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
|
||||
|
@ -1,13 +1,15 @@
|
||||
{
|
||||
"name": "eu.astrogd.white-leopard",
|
||||
"version": "1.0.0-alpha.1",
|
||||
"version": "1.0.0-alpha.5",
|
||||
"description": "A Discord bot that checks channel names for blacklisted words and reverts the changes if necessary",
|
||||
"main": "build/index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"build": "tsc && shx cp package-lock.json build/package-lock.json",
|
||||
"deploy-dev": "ts-node ci/devDeploy.ts",
|
||||
"start": "npm run build && npm run deploy-dev && docker-compose up --no-start && docker compose start database && node --enable-source-maps .",
|
||||
"deploy-commands:dev": "ts-node ci/devDeploy.ts",
|
||||
"deploy-commands:prod": "ts-node ci/deploy.ts",
|
||||
"deploy:dev": "npm run build && npm run deploy-commands:dev && docker compose build && docker compose up -d",
|
||||
"start": "npm run build && npm run deploy-commands:dev && docker-compose up --no-start && docker compose start database && node --enable-source-maps .",
|
||||
"migration:create": "node --require ts-node/register ./node_modules/typeorm/cli.js migration:generate -d src/data/dataSource.ts -p src/data/migrations/data",
|
||||
"migration:run": "node --require ts-node/register ./node_modules/typeorm/cli.js migration:run -d src/data/dataSource.ts",
|
||||
"migration:revert": "node --require ts-node/register ./node_modules/typeorm/cli.js migration:revert -d src/data/dataSource.ts",
|
||||
@ -36,6 +38,7 @@
|
||||
"discord.js": "^14.6.0",
|
||||
"dotenv": "^16.0.3",
|
||||
"fs-extra": "^10.1.0",
|
||||
"moment": "^2.29.4",
|
||||
"pg": "^8.8.0",
|
||||
"typeorm": "^0.3.10"
|
||||
}
|
||||
|
80
src/cli/global.ts
Normal file
80
src/cli/global.ts
Normal file
@ -0,0 +1,80 @@
|
||||
import { IsNull } from "typeorm";
|
||||
import { Badword, database } from "../data";
|
||||
import { Console } from "console";
|
||||
|
||||
const console = new Console(process.stdout);
|
||||
|
||||
export default async function execute(args: string[]) {
|
||||
const command = args[0];
|
||||
if (!command) return printHelp();
|
||||
|
||||
switch (command.toLowerCase()) {
|
||||
case "get": {
|
||||
const globalWords = await database.getRepository(Badword).find({
|
||||
where: {
|
||||
guildID: IsNull()
|
||||
}
|
||||
});
|
||||
|
||||
console.log(`Global blocked words:\n\n${globalWords.map(w => w.value).reduce((c, n) => c + ", " + n, "").slice(2)}`);
|
||||
break;
|
||||
}
|
||||
|
||||
case "add": {
|
||||
const word = args[1]?.toLowerCase();
|
||||
if (!word) return printHelp();
|
||||
|
||||
if (await database.getRepository(Badword).count({
|
||||
where: {
|
||||
value: word,
|
||||
guildID: IsNull()
|
||||
}
|
||||
}) > 0) return console.log(`${word} is already in the blocklist`);
|
||||
|
||||
const entity = new Badword();
|
||||
entity.value = word;
|
||||
|
||||
await database.getRepository(Badword).save(entity);
|
||||
console.log(`${word} has been added to the global block list`);
|
||||
break;
|
||||
}
|
||||
|
||||
case "remove": {
|
||||
const word = args[1]?.toLowerCase();
|
||||
if (!word) return printHelp();
|
||||
|
||||
await database.getRepository(Badword).delete({
|
||||
value: word,
|
||||
guildID: IsNull()
|
||||
});
|
||||
|
||||
console.log(`Removed ${word} from the global block list`);
|
||||
break;
|
||||
}
|
||||
|
||||
case "count": {
|
||||
const count = await database.getRepository(Badword).count({
|
||||
where: {
|
||||
guildID: IsNull()
|
||||
}
|
||||
});
|
||||
|
||||
console.log(`There are ${count} globally blocked words`);
|
||||
break;
|
||||
}
|
||||
|
||||
default: {
|
||||
printHelp();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function printHelp() {
|
||||
console.log(`Usage "global":
|
||||
|
||||
global get
|
||||
global add [WORD]
|
||||
global remove [WORD]
|
||||
global count`);
|
||||
}
|
150
src/cli/guild.ts
Normal file
150
src/cli/guild.ts
Normal file
@ -0,0 +1,150 @@
|
||||
import { getGuildSetting, isPremiumActive } from "../tools/data";
|
||||
import moment from "moment";
|
||||
import { Badword, database, GuildSetting } from "../data";
|
||||
import { Console } from "console";
|
||||
|
||||
const console = new Console(process.stdout);
|
||||
|
||||
export default async function execute(args: string[]) {
|
||||
const command = args[0];
|
||||
if (!command) return printHelp();
|
||||
|
||||
switch (command.toLowerCase()) {
|
||||
case "info": {
|
||||
if (!args[1]) return printHelp();
|
||||
const settings = await getGuildSetting(args[1]);
|
||||
const isPremium = isPremiumActive(settings.isPremiumUntil);
|
||||
const wordCount = await database.getRepository(Badword).count({
|
||||
where: {
|
||||
guildID: args[1]
|
||||
}
|
||||
});
|
||||
|
||||
console.log(`Guild ${args[1]}:
|
||||
- Premium: ${isPremium ? `ACTIVE for ${moment(settings.isPremiumUntil).fromNow(true)}` : "INACTIVE"}
|
||||
- Logchannel: ${settings.notificationChannelID ? `ENABLED (${settings.notificationChannelID})` : "DISABLED"}
|
||||
- blocked Words: ${wordCount}`);
|
||||
break;
|
||||
}
|
||||
|
||||
case "setpremium": {
|
||||
if (!args[1] || !args[2]) return printHelp();
|
||||
const settings = await getGuildSetting(args[1]);
|
||||
|
||||
if (args[2].toLowerCase() === "null") {
|
||||
settings.isPremiumUntil = null;
|
||||
await database.getRepository(GuildSetting).save(settings);
|
||||
console.log("Premium status removed for guild " + args[1]);
|
||||
break;
|
||||
}
|
||||
|
||||
const date = new Date(args[2]);
|
||||
if (isNaN(Number(date))) return printHelp();
|
||||
|
||||
const now = new Date();
|
||||
if (now > date) return console.log("Date lies in the past");
|
||||
|
||||
settings.isPremiumUntil = date;
|
||||
await database.getRepository(GuildSetting).save(settings);
|
||||
console.log(`Premium status for guild ${args[1]} is now active for ${moment(date).fromNow(true)}`);
|
||||
break;
|
||||
}
|
||||
|
||||
case "words": {
|
||||
if (!args[1] || !args[2]) return printWordHelp();
|
||||
|
||||
const badWords = await database.getRepository(Badword).find({
|
||||
where: {
|
||||
guildID: args[2]
|
||||
}
|
||||
});
|
||||
|
||||
switch (args[1].toLowerCase()) {
|
||||
case "get": {
|
||||
console.log(`Bad words for ${args[2]}:\n\n${badWords.map((badWord) => badWord.value).reduce((prev, next) => prev + ", " + next, "").slice(2)}`);
|
||||
break;
|
||||
}
|
||||
|
||||
case "add": {
|
||||
if (!args[3]) {
|
||||
printWordHelp();
|
||||
break;
|
||||
}
|
||||
|
||||
if (badWords.filter(w => w.value === args[3]!.toLowerCase()).length > 0) {
|
||||
console.log("Word already exists");
|
||||
break;
|
||||
}
|
||||
|
||||
const badWord = new Badword();
|
||||
badWord.guildID = args[2];
|
||||
badWord.value = args[3].toLowerCase();
|
||||
|
||||
await database.getRepository(Badword).save(badWord);
|
||||
console.log(`${args[3].toLowerCase()} added to guild ${args[2]}`);
|
||||
break;
|
||||
}
|
||||
|
||||
case "remove": {
|
||||
if (!args[3]) {
|
||||
printWordHelp();
|
||||
break;
|
||||
}
|
||||
|
||||
const badWord = badWords.find((w) => w.value === args[3]?.toLowerCase());
|
||||
|
||||
if (!badWord) {
|
||||
console.log(`${args[3].toLowerCase()} is not in blocklist of guild ${args[2]}`);
|
||||
break;
|
||||
}
|
||||
|
||||
await database.getRepository(Badword).delete({
|
||||
id: badWord.id
|
||||
});
|
||||
|
||||
console.log(`${badWord.value} deleted for guild ${args[2]}`);
|
||||
break;
|
||||
}
|
||||
|
||||
case "clear": {
|
||||
await database.getRepository(Badword).delete({
|
||||
guildID: args[2]
|
||||
});
|
||||
|
||||
console.log(`Deleted ${badWords.length} entries`);
|
||||
break;
|
||||
}
|
||||
|
||||
default: {
|
||||
printHelp();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
default: {
|
||||
printHelp();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function printHelp() {
|
||||
console.log(`Usage "guild":
|
||||
|
||||
guild info [GUILDID]
|
||||
guild setPremium [GUILDID] [YYYY-MM-DD or NULL]
|
||||
guild words [get|add|remove|clear]`);
|
||||
}
|
||||
|
||||
function printWordHelp() {
|
||||
console.log(`Usage "guild words":
|
||||
|
||||
guild words get [GUILDID]
|
||||
guild words add [GUILDID] [WORD]
|
||||
guild words remove [GUILDID] [WORD]
|
||||
guild words clear [GUILDID]`);
|
||||
}
|
68
src/cli/index.ts
Normal file
68
src/cli/index.ts
Normal file
@ -0,0 +1,68 @@
|
||||
import * as readline from "node:readline/promises";
|
||||
import { stdin as input, stdout as output } from "node:process";
|
||||
import pack from "../../package.json";
|
||||
import { Console } from "node:console";
|
||||
import moment from "moment";
|
||||
|
||||
import guild from "./guild";
|
||||
import global from "./global";
|
||||
|
||||
const console = new Console(process.stdout);
|
||||
|
||||
const startupTime = new Date();
|
||||
const rl = readline.createInterface(input, output);
|
||||
|
||||
rl.on("line", async (msg) => {
|
||||
const [command, ...args] = msg.split(" ");
|
||||
if (!command) return;
|
||||
|
||||
switch (command.toLowerCase()) {
|
||||
case "version": {
|
||||
console.log(`Channel filter V${pack.version} by AstroGD®`);
|
||||
break;
|
||||
}
|
||||
|
||||
case "uptime": {
|
||||
console.log(`Application is running for ${moment(startupTime).fromNow(true)}`);
|
||||
break;
|
||||
}
|
||||
|
||||
case "clear": {
|
||||
console.clear();
|
||||
break;
|
||||
}
|
||||
|
||||
case "guild": {
|
||||
await guild(args);
|
||||
break;
|
||||
}
|
||||
|
||||
case "help": {
|
||||
printHelp();
|
||||
break;
|
||||
}
|
||||
|
||||
case "global": {
|
||||
await global(args);
|
||||
break;
|
||||
}
|
||||
|
||||
default: {
|
||||
console.log(`Unknown command. Try "help" for help`);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
rl.prompt();
|
||||
});
|
||||
|
||||
function printHelp() {
|
||||
console.log(`Commands:
|
||||
|
||||
version
|
||||
uptime
|
||||
guild
|
||||
global
|
||||
help
|
||||
clear`);
|
||||
}
|
197
src/commands/blocklist.ts
Normal file
197
src/commands/blocklist.ts
Normal file
@ -0,0 +1,197 @@
|
||||
import { ChatInputCommandInteraction, PermissionFlagsBits, SlashCommandBuilder } from "discord.js";
|
||||
import { database, Badword } from "../data";
|
||||
import { IsNull } from "typeorm";
|
||||
import { getGuildSetting, isPremiumActive } from "../tools/data";
|
||||
import getDefaultEmbed, { getFailedEmbed, getSuccessEmbed } from "../tools/defaultEmbeds";
|
||||
import { getGuildChannel } from "../tools/discord";
|
||||
import { Color, Emoji } from "../tools/design";
|
||||
|
||||
const builder = new SlashCommandBuilder();
|
||||
builder.setName("blocklist");
|
||||
builder.setDescription("Configures the servers blocklist");
|
||||
builder.setDefaultMemberPermissions(PermissionFlagsBits.ManageGuild);
|
||||
builder.setDMPermission(false);
|
||||
builder.addSubcommand((builder) => {
|
||||
builder.setName("add");
|
||||
builder.setDescription("Adds a word to the servers blocklist");
|
||||
builder.addStringOption((option) => {
|
||||
option.setName("word");
|
||||
option.setDescription("The word to add");
|
||||
option.setRequired(true);
|
||||
option.setMaxLength(50);
|
||||
return option;
|
||||
});
|
||||
return builder;
|
||||
});
|
||||
builder.addSubcommand((builder) => {
|
||||
builder.setName("remove");
|
||||
builder.setDescription("Removes a word from the servers blocklist");
|
||||
builder.addStringOption((option) => {
|
||||
option.setName("word");
|
||||
option.setDescription("The word to remove");
|
||||
option.setRequired(true);
|
||||
option.setMaxLength(50);
|
||||
return option;
|
||||
});
|
||||
return builder;
|
||||
});
|
||||
builder.addSubcommand((builder) => {
|
||||
builder.setName("get");
|
||||
builder.setDescription("Returns all words from the servers blocklist");
|
||||
return builder;
|
||||
});
|
||||
|
||||
async function execute(interaction: ChatInputCommandInteraction): Promise<void> {
|
||||
if (!interaction.guildId) throw new Error("Command was executed in DM context");
|
||||
|
||||
const settings = await getGuildSetting(interaction.guildId);
|
||||
const isPremium = isPremiumActive(settings.isPremiumUntil);
|
||||
|
||||
const logChannel = settings.notificationChannelID ? await getGuildChannel(interaction.guildId, settings.notificationChannelID) : null;
|
||||
|
||||
switch (interaction.options.getSubcommand(true)) {
|
||||
case "get": {
|
||||
const guildBadWords = await database.getRepository(Badword).find({
|
||||
select: {
|
||||
value: true
|
||||
},
|
||||
where: {
|
||||
guildID: interaction.guildId
|
||||
}
|
||||
});
|
||||
const globalBadWords = await database.getRepository(Badword).find({
|
||||
where: {
|
||||
guildID: IsNull()
|
||||
}
|
||||
});
|
||||
|
||||
interaction.reply({
|
||||
content: `\`\`\`Global bad word list\`\`\`\n||${globalBadWords.map((word) => word.value).reduce((prev, next) => prev + ", " + next, "").slice(2)} ||\n\`\`\`Local server bad word list (${guildBadWords.length}/${isPremium ? 100 : 10})\`\`\`\n||${guildBadWords.map((word) => word.value).reduce((prev, next) => prev + ", " + next, "").slice(2)} ||`,
|
||||
ephemeral: true
|
||||
}).catch();
|
||||
break;
|
||||
}
|
||||
|
||||
case "add": {
|
||||
const count = await database.getRepository(Badword).count({
|
||||
where: {
|
||||
guildID: interaction.guildId
|
||||
}
|
||||
});
|
||||
|
||||
const limit = isPremium ? 100 : 10;
|
||||
if (count >= limit) {
|
||||
const embed = getFailedEmbed();
|
||||
embed.setDescription(`You reached the word limit for your guild. Please delete a word before adding a new one`);
|
||||
interaction.reply({
|
||||
embeds: [embed],
|
||||
ephemeral: true
|
||||
}).catch();
|
||||
return;
|
||||
}
|
||||
|
||||
const word = interaction.options.getString("word", true).toLowerCase();
|
||||
|
||||
const exists = await database.getRepository(Badword).count({
|
||||
where: [
|
||||
{guildID: interaction.guildId, value: word},
|
||||
{guildID: IsNull(), value: word}
|
||||
]
|
||||
}) > 0;
|
||||
|
||||
if (exists) {
|
||||
const embed = getFailedEmbed();
|
||||
embed.setDescription(`"${word}" already exists in the blocklist`);
|
||||
interaction.reply({
|
||||
embeds: [embed],
|
||||
ephemeral: true
|
||||
}).catch();
|
||||
return;
|
||||
}
|
||||
|
||||
const entry = new Badword();
|
||||
entry.guildID = interaction.guildId;
|
||||
entry.value = word;
|
||||
|
||||
await database.getRepository(Badword).save(entry);
|
||||
|
||||
const embed = getSuccessEmbed();
|
||||
embed.setDescription(`"${word}" has been added to the blocklist`);
|
||||
interaction.reply({
|
||||
embeds: [embed],
|
||||
ephemeral: true
|
||||
});
|
||||
|
||||
const logMessage = getDefaultEmbed();
|
||||
logMessage.setTitle(`${Emoji.SETTINGS} Word added`);
|
||||
logMessage.setColor(Color.INFORMING_BLUE);
|
||||
logMessage.setDescription(`"||${word}||" has been added to the blocklist`);
|
||||
logMessage.addFields({
|
||||
name: "This action was performed by",
|
||||
value: `${interaction.user.tag} (${interaction.user.id})`
|
||||
});
|
||||
if (logChannel && logChannel.isTextBased()) {
|
||||
logChannel.send({
|
||||
embeds: [logMessage]
|
||||
}).catch();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case "remove": {
|
||||
const word = interaction.options.getString("word", true).toLowerCase();
|
||||
|
||||
const entry = await database.getRepository(Badword).findOne({
|
||||
where: {
|
||||
guildID: interaction.guildId,
|
||||
value: word
|
||||
}
|
||||
});
|
||||
|
||||
if (!entry) {
|
||||
const embed = getFailedEmbed();
|
||||
embed.setDescription(`"${word}" was not found in the blocklist`);
|
||||
interaction.reply({
|
||||
embeds: [embed],
|
||||
ephemeral: true
|
||||
}).catch();
|
||||
return;
|
||||
}
|
||||
|
||||
await database.getRepository(Badword).delete({
|
||||
id: entry.id
|
||||
});
|
||||
|
||||
const embed = getSuccessEmbed();
|
||||
embed.setDescription(`"${word}" has been removed from the blocklist`);
|
||||
interaction.reply({
|
||||
embeds: [embed],
|
||||
ephemeral: true
|
||||
}).catch();
|
||||
|
||||
const logMessage = getDefaultEmbed();
|
||||
logMessage.setTitle(`${Emoji.SETTINGS} Word removed`);
|
||||
logMessage.setColor(Color.INFORMING_BLUE);
|
||||
logMessage.setDescription(`"||${word}||" has been removed from the blocklist`);
|
||||
logMessage.addFields({
|
||||
name: "This action was performed by",
|
||||
value: `${interaction.user.tag} (${interaction.user.id})`
|
||||
});
|
||||
if (logChannel && logChannel.isTextBased()) {
|
||||
logChannel.send({
|
||||
embeds: [logMessage]
|
||||
}).catch();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default: {
|
||||
throw new Error(`"${interaction.options.getSubcommand(true)}" cannot be executed`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
builder,
|
||||
execute
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
import * as notification from "./notification";
|
||||
import * as blocklist from "./blocklist";
|
||||
import * as info from "./info";
|
||||
|
||||
const array = [notification.builder.toJSON()];
|
||||
const array = [notification.builder.toJSON(), blocklist.builder.toJSON(), info.builder.toJSON()];
|
||||
|
||||
export {
|
||||
array
|
||||
|
@ -1,10 +1,14 @@
|
||||
import { ChatInputCommandInteraction, Collection, Events, SlashCommandBuilder } from "discord.js";
|
||||
import * as notification from "./notification";
|
||||
import * as blocklist from "./blocklist";
|
||||
import * as info from "./info";
|
||||
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);
|
||||
commands.set(blocklist.builder.name, blocklist);
|
||||
commands.set(info.builder.name, info);
|
||||
|
||||
client.on(Events.InteractionCreate, async (interaction) => {
|
||||
if (!interaction.isChatInputCommand()) return;
|
||||
|
66
src/commands/info.ts
Normal file
66
src/commands/info.ts
Normal file
@ -0,0 +1,66 @@
|
||||
import { ChatInputCommandInteraction, SlashCommandBuilder } from "discord.js";
|
||||
import { IsNull } from "typeorm";
|
||||
import { Badword, database } from "../data";
|
||||
import { getGuildSetting, isPremiumActive } from "../tools/data";
|
||||
import getDefaultEmbed from "../tools/defaultEmbeds";
|
||||
import pack from "../../package.json";
|
||||
import { Color, Emoji } from "../tools/design";
|
||||
|
||||
const builder = new SlashCommandBuilder();
|
||||
builder.setName("info");
|
||||
builder.setDescription("Shows information about this bot and the server settings");
|
||||
builder.setDMPermission(false);
|
||||
|
||||
async function execute(interaction: ChatInputCommandInteraction): Promise<void> {
|
||||
if (!interaction.inGuild()) throw new Error("Command was executed outside guild context");
|
||||
|
||||
const settings = await getGuildSetting(interaction.guildId);
|
||||
const isPremium = isPremiumActive(settings.isPremiumUntil);
|
||||
const globalBlockedWordsCount = await database.getRepository(Badword).count({
|
||||
where: {
|
||||
guildID: IsNull()
|
||||
}
|
||||
});
|
||||
const localBlockedWordsCount = await database.getRepository(Badword).count({
|
||||
where: {
|
||||
guildID: interaction.guildId
|
||||
}
|
||||
});
|
||||
|
||||
const embed = getDefaultEmbed();
|
||||
embed.setTitle(`${Emoji.SECURITY_CHALLENGE} Channel filter V${pack.version} by AstroGD®`);
|
||||
embed.setDescription(`Codename eu.astrogd.white-leopard`);
|
||||
embed.setColor(isPremium ? Color.PREMIUM_ORANGE : Color.INFORMING_BLUE);
|
||||
embed.addFields({
|
||||
name: "What does this bot do?",
|
||||
value: "This bot checks for blocked words contained in channel names and reverts the changes if found."
|
||||
},{
|
||||
name: "Author",
|
||||
value: `This bot was created by AstroGD#0001 ${Emoji.ASTROGD} mainly for the official r/Overwatch2 Subreddit Discord server`
|
||||
},{
|
||||
name: "Server status",
|
||||
value: `${isPremium ? Emoji.PREMIUM : Emoji.SWITCH_OFF} Premium features are ${isPremium ? "enabled" : "disabled"} on this server`,
|
||||
inline: true
|
||||
},{
|
||||
name: "Global word count",
|
||||
value: globalBlockedWordsCount.toString(),
|
||||
inline: true
|
||||
},{
|
||||
name: "Local word count",
|
||||
value: localBlockedWordsCount.toString(),
|
||||
inline: true
|
||||
},{
|
||||
name: `${Emoji.WAVING} Have a question or want to say hello?`,
|
||||
value: "Join the support Discord server at https://go.astrogd.eu/discord"
|
||||
});
|
||||
|
||||
interaction.reply({
|
||||
embeds: [embed],
|
||||
ephemeral: true
|
||||
}).catch();
|
||||
}
|
||||
|
||||
export {
|
||||
builder,
|
||||
execute
|
||||
}
|
@ -1,7 +1,8 @@
|
||||
import { SlashCommandBuilder, PermissionFlagsBits, ChannelType, ChatInputCommandInteraction, NewsChannel, TextBasedChannel, CategoryChannel, StageChannel, TextChannel, PrivateThreadChannel, PublicThreadChannel, VoiceChannel, APIInteractionDataResolvedChannel, ForumChannel, GuildBasedChannel } from "discord.js";
|
||||
import { SlashCommandBuilder, PermissionFlagsBits, ChannelType, ChatInputCommandInteraction, NewsChannel, TextBasedChannel, CategoryChannel, StageChannel, TextChannel, PrivateThreadChannel, PublicThreadChannel, VoiceChannel, APIInteractionDataResolvedChannel, ForumChannel } from "discord.js";
|
||||
import getDefaultEmbed, { getSuccessEmbed } from "../tools/defaultEmbeds";
|
||||
import { database, GuildSetting } from "../data";
|
||||
import client from "../client";
|
||||
import { getGuildSetting } from "../tools/data";
|
||||
import { getGuildChannel } from "../tools/discord";
|
||||
|
||||
const builder = new SlashCommandBuilder();
|
||||
builder.setName("logchannel");
|
||||
@ -16,31 +17,6 @@ builder.addChannelOption((option) => {
|
||||
return option;
|
||||
});
|
||||
|
||||
async function getGuildSetting(guildID: string): Promise<GuildSetting> {
|
||||
let guildSetting = await database.getRepository(GuildSetting).findOne({
|
||||
where: {
|
||||
id: guildID
|
||||
}
|
||||
});
|
||||
|
||||
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;
|
||||
|
||||
|
26
src/data/migrations/1669300160536-data.ts
Normal file
26
src/data/migrations/1669300160536-data.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import { MigrationInterface, QueryRunner } from "typeorm";
|
||||
|
||||
export class data1669300160536 implements MigrationInterface {
|
||||
name = 'data1669300160536'
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`
|
||||
ALTER TABLE "guild_setting" DROP COLUMN "isPremiumUntil"
|
||||
`);
|
||||
await queryRunner.query(`
|
||||
ALTER TABLE "guild_setting"
|
||||
ADD "isPremiumUntil" TIMESTAMP
|
||||
`);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`
|
||||
ALTER TABLE "guild_setting" DROP COLUMN "isPremiumUntil"
|
||||
`);
|
||||
await queryRunner.query(`
|
||||
ALTER TABLE "guild_setting"
|
||||
ADD "isPremiumUntil" date
|
||||
`);
|
||||
}
|
||||
|
||||
}
|
@ -8,6 +8,6 @@ export class GuildSetting {
|
||||
@Column("varchar", { nullable: true, default: null })
|
||||
notificationChannelID!: string | null;
|
||||
|
||||
@Column("date", { nullable: true, default: null })
|
||||
@Column("timestamp", { nullable: true, default: null })
|
||||
isPremiumUntil!: Date | null;
|
||||
}
|
85
src/events/channelUpdate.ts
Normal file
85
src/events/channelUpdate.ts
Normal file
@ -0,0 +1,85 @@
|
||||
import client from "../client";
|
||||
import { Events } from "discord.js";
|
||||
import { getGuildSetting, isPremiumActive } from "../tools/data";
|
||||
import { Badword, database } from "../data";
|
||||
import { IsNull } from "typeorm";
|
||||
import getDefaultEmbed, { getFailedEmbed } from "../tools/defaultEmbeds";
|
||||
import { getGuildChannel } from "../tools/discord";
|
||||
import { Color, Emoji } from "../tools/design";
|
||||
|
||||
client.on(Events.ChannelUpdate, async (oldChannel, newChannel) => {
|
||||
if (oldChannel.isDMBased() || newChannel.isDMBased()) return;
|
||||
const name = newChannel.name.toLowerCase();
|
||||
if (name === "censored") return;
|
||||
|
||||
const guild = oldChannel.guild;
|
||||
const settings = await getGuildSetting(guild.id);
|
||||
const isPremium = isPremiumActive(settings.isPremiumUntil);
|
||||
|
||||
const globalBlocklist = await database.getRepository(Badword).find({
|
||||
where: {
|
||||
guildID: IsNull()
|
||||
}
|
||||
});
|
||||
const localBlocklist = await database.getRepository(Badword).find({
|
||||
where: {
|
||||
guildID: guild.id
|
||||
}
|
||||
});
|
||||
|
||||
const blocklist = [...globalBlocklist, ...localBlocklist];
|
||||
let found: string | null = null;
|
||||
|
||||
for (let i = 0; i < blocklist.length; i++) {
|
||||
const word = blocklist[i];
|
||||
if (!word) continue;
|
||||
|
||||
if (!name.includes(word.value)) continue;
|
||||
found = word.value;
|
||||
break;
|
||||
}
|
||||
|
||||
if (found === null) return;
|
||||
|
||||
const logChannel = settings.notificationChannelID ? await getGuildChannel(guild.id, settings.notificationChannelID) : null;
|
||||
|
||||
try {
|
||||
await newChannel.setName("CENSORED", `[Automated] Detected blocked word in channel name. Name has been censored`);
|
||||
} catch (error) {
|
||||
if (!logChannel || !logChannel.isTextBased()) return;
|
||||
const embed = getFailedEmbed();
|
||||
embed.setDescription(`Couldn't censor <#${newChannel.id}> (${newChannel.id}): ${error instanceof Error ? error.message : error}`);
|
||||
if (isPremium) embed.addFields({
|
||||
name: "Detected banned word:",
|
||||
value: `||${found}||`
|
||||
},{
|
||||
name: "Old channel name:",
|
||||
value: `||${name}||`,
|
||||
inline: true
|
||||
});
|
||||
|
||||
logChannel.send({
|
||||
embeds: [embed]
|
||||
}).catch();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!logChannel || !logChannel.isTextBased()) return;
|
||||
const embed = getDefaultEmbed();
|
||||
embed.setTitle(`${Emoji.SECURITY_CHALLENGE_FAILED} Blocked word detected`);
|
||||
embed.setDescription(`<#${newChannel.id}> (${newChannel.id}) has been renamed because its name contained a blocked word.`);
|
||||
embed.setColor(Color.WARNING_YELLOW);
|
||||
if (isPremium) embed.addFields({
|
||||
name: "Detected banned word:",
|
||||
value: `||${found}||`,
|
||||
inline: true
|
||||
},{
|
||||
name: "Old channel name:",
|
||||
value: `||${name}||`,
|
||||
inline: true
|
||||
});
|
||||
|
||||
logChannel.send({
|
||||
embeds: [embed]
|
||||
}).catch();
|
||||
});
|
1
src/events/index.ts
Normal file
1
src/events/index.ts
Normal file
@ -0,0 +1 @@
|
||||
import "./channelUpdate";
|
26
src/tools/data.ts
Normal file
26
src/tools/data.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import { database, GuildSetting } from "../data";
|
||||
|
||||
export async function getGuildSetting(guildID: string): Promise<GuildSetting> {
|
||||
let guildSetting = await database.getRepository(GuildSetting).findOne({
|
||||
where: {
|
||||
id: guildID
|
||||
}
|
||||
});
|
||||
|
||||
if (!guildSetting) {
|
||||
guildSetting = new GuildSetting();
|
||||
guildSetting.id = guildID;
|
||||
guildSetting.isPremiumUntil = null;
|
||||
guildSetting.notificationChannelID = null;
|
||||
}
|
||||
|
||||
return guildSetting;
|
||||
}
|
||||
|
||||
export function isPremiumActive(timestamp: Date | null): boolean {
|
||||
if (timestamp === null) return false;
|
||||
const now = Number(new Date());
|
||||
const activeUntil = Number(timestamp);
|
||||
|
||||
return now < activeUntil;
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
import pack from "../../package.json";
|
||||
import { EmbedBuilder } from "discord.js";
|
||||
import client from "../client";
|
||||
import { Color, Emoji } from "./design";
|
||||
|
||||
// const _coolColors = [0x054566];
|
||||
|
||||
@ -11,14 +12,21 @@ export default function getDefaultEmbed(): EmbedBuilder {
|
||||
iconURL: client.user?.avatarURL() || undefined,
|
||||
});
|
||||
embed.setTimestamp(new Date());
|
||||
embed.setColor(0x3682cc);
|
||||
embed.setColor(Color.ANONYMOUS_GRAY);
|
||||
|
||||
return embed;
|
||||
}
|
||||
|
||||
export function getSuccessEmbed(): EmbedBuilder {
|
||||
const embed = getDefaultEmbed();
|
||||
embed.setTitle("Success");
|
||||
embed.setColor(0x32d122);
|
||||
embed.setTitle(`${Emoji.CHAT_APPROVE} Success`);
|
||||
embed.setColor(Color.SUCCESSFUL_GREEN);
|
||||
return embed;
|
||||
}
|
||||
|
||||
export function getFailedEmbed(): EmbedBuilder {
|
||||
const embed = getDefaultEmbed();
|
||||
embed.setTitle(`${Emoji.CHAT_DENY} Failed`);
|
||||
embed.setColor(Color.STOPSIGN_RED);
|
||||
return embed;
|
||||
}
|
30
src/tools/design.ts
Normal file
30
src/tools/design.ts
Normal file
@ -0,0 +1,30 @@
|
||||
export enum Emoji {
|
||||
DOUBLE_ARROW_RIGHT = "<:double_arrow_right:918922668936413267>",
|
||||
SETTINGS = "<:settings:918912063970099232>",
|
||||
TIME = "<:time:918913616743387156>",
|
||||
DOCS = "<:docs:918917779283918899>",
|
||||
MESSAGE = "<:message:918920872683786280>",
|
||||
WAVING = "<:waving:918949572804505640>",
|
||||
CHAT_APPROVE = "<:chat_approve:918910607317667860>",
|
||||
CHAT_DENY = "<:chat_deny:918911411663544410>",
|
||||
WARN = "<:warn:918914600181825556>",
|
||||
INFORMATION = "<:information:918912973874028614>",
|
||||
ERROR = "<:error:918915254447136841>",
|
||||
SWITCH_ON = "<:switch_on:918915977662586892>",
|
||||
SWITCH_OFF = "<:switch_off:918917065899925584>",
|
||||
SWITCH_UNSET = "<:switch_unset:918917082807156796>",
|
||||
SECURITY_CHALLENGE = "<:security_challenge:918919903405305946>",
|
||||
SECURITY_CHALLENGE_SUCCESS = "<:security_challenge_success:918919918672576562>",
|
||||
SECURITY_CHALLENGE_FAILED = "<:security_challenge_failed:918919932887064696>",
|
||||
ASTROGD = "<:astrogd:918906741125697626>",
|
||||
PREMIUM = "<:premium:918909591255908442>",
|
||||
}
|
||||
|
||||
export enum Color {
|
||||
PREMIUM_ORANGE = 0xFFC800,
|
||||
SUCCESSFUL_GREEN = 0x77DE37,
|
||||
STOPSIGN_RED = 0xDA2132,
|
||||
WARNING_YELLOW = 0xF0E210,
|
||||
INFORMING_BLUE = 0x2FAAE2,
|
||||
ANONYMOUS_GRAY = 0x7B7B7B
|
||||
}
|
10
src/tools/discord.ts
Normal file
10
src/tools/discord.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { GuildBasedChannel } from "discord.js";
|
||||
import client from "../client";
|
||||
|
||||
export 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;
|
||||
}
|
Reference in New Issue
Block a user