Compare commits
5 Commits
1.0.0-alph
...
1.0.0-beta
Author | SHA1 | Date | |
---|---|---|---|
c8ed870fec | |||
f8d029431f
|
|||
cd60d7dabc
|
|||
62aa05031f
|
|||
dc6755eb19
|
21
README.md
21
README.md
@ -15,15 +15,20 @@ 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 |
|
||||
| :---------- | :------------------------------------------------------------ | :------: | :------------------ |
|
||||
| TOKEN | Discord bot token to log into the API with | ▶️/🚀 | NzYzMDP3MzE1Mzky... |
|
||||
| DB_HOST | Hostname of the database | ▶️ | 127.0.0.1:3546 |
|
||||
| DB_USERNAME | Username of the database | ▶️ | root |
|
||||
| DB_PASSWORD | Password of the database | ▶️ | abc123 |
|
||||
| DB_DATABASE | Database name | ▶️ | white-leopard |
|
||||
| CLIENT_ID | Client ID of the Discord appication associated with the token | 🚀 | 763035392692274 |
|
||||
| Name | Description | Required | Example |
|
||||
| :--------------- | :------------------------------------------------------------------------------ | :------: | :------------------ |
|
||||
| TOKEN | Discord bot token to log into the API with | ▶️/🚀 | NzYzMDP3MzE1Mzky... |
|
||||
| DB_HOST | Hostname of the database | ▶️ | 127.0.0.1:3546 |
|
||||
| DB_USERNAME | Username of the database | ▶️ | root |
|
||||
| DB_PASSWORD | Password of the database | ▶️ | abc123 |
|
||||
| DB_DATABASE | Database name | ▶️ | white-leopard |
|
||||
| CLIENT_ID | Client ID of the Discord appication associated with the token | 🚀 | 763035392692274 |
|
||||
| DEPLOY_TOKEN | Production Discord bot token to log into the API with | 🚀 | NzYzMDP3MzE1Mzky... |
|
||||
| DEPLOY_CLIENT_ID | Production Client ID of the Discord appication associated with the deploy token | 🚀 | 763035392692274 |
|
||||
|
||||
### Icon explanation:
|
||||
- ▶️ = Required in runtime
|
||||
|
@ -4,8 +4,8 @@ import dotenv from "dotenv";
|
||||
dotenv.config({ path: path.join(__dirname, "../.env") });
|
||||
|
||||
// Environment checking
|
||||
const TOKEN = process.env["TOKEN"];
|
||||
const CLIENT_ID = process.env["CLIENT_ID"];
|
||||
const TOKEN = process.env["DEPLOY_TOKEN"];
|
||||
const CLIENT_ID = process.env["DEPLOY_CLIENT_ID"];
|
||||
|
||||
if (!TOKEN) throw new ReferenceError("Environment variable TOKEN is missing");
|
||||
if (!CLIENT_ID) throw new ReferenceError("Environment variable CLIENT_ID is missing");
|
||||
|
@ -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
|
||||
|
5
index.ts
5
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() {
|
||||
@ -15,4 +17,5 @@ function shutdown() {
|
||||
}
|
||||
|
||||
process.on("SIGINT", shutdown);
|
||||
process.on("SIGHUP", 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-beta.1",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "eu.astrogd.white-leopard",
|
||||
"version": "1.0.0-alpha.1",
|
||||
"version": "1.0.0-beta.1",
|
||||
"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",
|
||||
|
10
package.json
10
package.json
@ -1,13 +1,16 @@
|
||||
{
|
||||
"name": "eu.astrogd.white-leopard",
|
||||
"version": "1.0.0-alpha.3",
|
||||
"version": "1.0.0-beta.1",
|
||||
"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",
|
||||
"deploy:prod": "rimraf build && npm run build && npm run deploy-commands:prod && docker build -t astrogd/white-leopard:latest . && docker push astrogd/white-leopard:latest",
|
||||
"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 +39,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`);
|
||||
}
|
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";
|
Reference in New Issue
Block a user