Compare commits
82 Commits
1.0.0-alph
...
main
Author | SHA1 | Date | |
---|---|---|---|
b485b5d420 | |||
1c50e818b0 | |||
8cb74a1291 | |||
4bf782cddf | |||
c6a4577872 | |||
3594c2ea8c | |||
538d039c5c | |||
da1996f2ff | |||
d5b75b12c8 | |||
|
a71dbecf8c | ||
|
f528d107da | ||
9510116a37 | |||
b8508a4a82 | |||
ccf2fd9361 | |||
cb4bddf983 | |||
b1b087e32c | |||
451b414fba | |||
|
b53c08a553 | ||
f34e06d31f | |||
173438395e | |||
59429ea548 | |||
dbb64baae3 | |||
eccb379b51 | |||
cb5516246f | |||
|
63c67f4992 | ||
|
b24cc4eba8 | ||
4466c138ab | |||
44e5cc8c91 | |||
fc9f1c7c4c | |||
6be983b217 | |||
|
781d2e547e | ||
ddf28c1612 | |||
5fb9ad1e12 | |||
bfabd62674 | |||
5eb56efb79 | |||
1c6c79dacf | |||
f4b7685f94 | |||
c3b0a4316b | |||
ee9039c239 | |||
361326c31e | |||
f7c878854b | |||
a8cd924e18 | |||
3a6641be86 | |||
c9df84b255 | |||
0d69cf3e7b | |||
730151e34b | |||
|
d4457e89ce | ||
|
11845cae42 | ||
d853fa55d3 | |||
88834703ef | |||
|
9cbb7799e9 | ||
005b9bd69c | |||
21812157f8 | |||
52a349c1b9 | |||
|
6242c09e4b | ||
24d39d471e | |||
545bda9bee | |||
966625bac4 | |||
|
cc50a0a089 | ||
27f625155e | |||
8b72326419 | |||
d9c81ef3f2 | |||
71b401dbee | |||
fda6166483 | |||
df2548dba6 | |||
ca29f047e9 | |||
7a5d7f46b6 | |||
0c9000a48c | |||
|
fb2e3c4f11 | ||
657a58f3a5 | |||
f695f0162f | |||
4fef4e8045 | |||
df8d186139 | |||
f7727c3542 | |||
7be72df267 | |||
|
c8ed870fec | ||
f8d029431f | |||
cd60d7dabc | |||
62aa05031f | |||
dc6755eb19 | |||
def44d2774 | |||
a26fb2df12 |
3
.npmrc
Normal file
3
.npmrc
Normal file
@ -0,0 +1,3 @@
|
||||
@astrogd:registry=https://git.astrogd.cloud/api/packages/packages/npm/
|
||||
@internal:registry=https://git.astrogd.cloud/api/packages/internal/npm/
|
||||
//git.astrogd.cloud/api/packages/internal/npm/:_authToken=${NPM_TOKEN}
|
43
Changelog.md
Normal file
43
Changelog.md
Normal file
@ -0,0 +1,43 @@
|
||||
# Changelog
|
||||
|
||||
This file is used to list changes made to this software.
|
||||
|
||||
_Current development release: 1.1.1_
|
||||
|
||||
## V1.1.1 [2023-09-16]
|
||||
|
||||
### Updates
|
||||
- Updated packages to latest versions
|
||||
|
||||
### Bugfixes
|
||||
- Fixed a bug where the bot would crash on startup if the database connection establishment took too long
|
||||
|
||||
### Other
|
||||
- Renamed db service in docker compose file from database to db
|
||||
|
||||
## V1.1.0 [2022-11-29]
|
||||
|
||||
### Features
|
||||
|
||||
- If a channel gets deleted and the responsible user has been detected, a message will be sent to that user informing him of the deletion
|
||||
|
||||
## V1.0.0 [2022-11-29]
|
||||
|
||||
### Features
|
||||
|
||||
- /info
|
||||
- /logchannel
|
||||
- /blocklist get
|
||||
- /blocklist add
|
||||
- /blocklist remove
|
||||
- /preservesettings
|
||||
- /showblocklist
|
||||
- /showsettings
|
||||
- Data will be deleted by default when the bot leaves the server
|
||||
- Server admins can change the behaviour of the bot when it leaves the server to keep the data persistent
|
||||
- Scans for blocked words in channel names and deletes the channel if found
|
||||
- Gets the user creating or renaming a channel to a blocked word
|
||||
- When settings are changed or channels are deleted, notifications to a logchannel can be enabled
|
||||
- CLI to change settings and get information during runtime by attaching to the apps docker container
|
||||
- API for automated uptime checks to prevent the bot from going offline unnoticed
|
||||
- Bot notifies admins via /showsettings when it lacks permissions needed for its functionality
|
@ -10,4 +10,5 @@ ENV NODE_ENV=production
|
||||
COPY --from=builder node_modules ./node_modules
|
||||
COPY build/ .
|
||||
VOLUME [ "/usr/src/app/data" ]
|
||||
EXPOSE 80
|
||||
CMD ["node", "--enable-source-maps", "index.js"]
|
49
README.md
49
README.md
@ -1,20 +1,61 @@
|
||||
# eu.astrogd.white-leopard
|
||||
|
||||
A Discord bot that checks Discord channel names for banned words and prevents renaming of them
|
||||
|
||||
## Commands
|
||||
### /logchanel [channel?]
|
||||
|
||||
### /logchanel [channel?] `Permission: MANAGE_GUILD`
|
||||
|
||||
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 `Permission: MANAGE_GUILD`
|
||||
|
||||
#### /blocklist get
|
||||
|
||||
Returns the global and server specific banned word list and returns it (Same behaviour as /showblocklist)
|
||||
|
||||
#### /blocklist add [word]
|
||||
|
||||
Adds the word to the server specific blocklist
|
||||
|
||||
#### /blocklist remove [word]
|
||||
|
||||
Removes the word from the server specific blocklist
|
||||
|
||||
### /info `Permission: EVERYONE`
|
||||
|
||||
Returns general information about the bot and the servers stats
|
||||
|
||||
### /preservesettings `Permission: ADMINISTRATOR`
|
||||
|
||||
Changes the behaviour when the bot leaves the server.
|
||||
Options are:
|
||||
|
||||
- Keep settings persistent even if the bot leaves
|
||||
- Delete setting when the bot leaves the server [default]
|
||||
|
||||
### /showblocklist `Permission: MANAGE_CHANNELS`
|
||||
|
||||
Returns the global and server specific banned word list and returns it
|
||||
|
||||
### /showsettings `Permission: MANAGE_GUILD`
|
||||
|
||||
Returns the current settings for the server
|
||||
|
||||
## 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_HOST | Hostname of the database | ▶️ | 127.0.0.1 |
|
||||
| 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
|
||||
|
||||
### Icon explanation:
|
||||
- ▶️ = Required in runtime
|
||||
- 🚀 = Required during CI/CD
|
@ -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,18 +1,23 @@
|
||||
version: "2"
|
||||
version: "3.9"
|
||||
services:
|
||||
app:
|
||||
image: astrogd/white-leopard:dev
|
||||
build: ./
|
||||
ports:
|
||||
- 80:80
|
||||
tty: true
|
||||
stdin_open: true
|
||||
depends_on:
|
||||
- database
|
||||
- db
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
- TOKEN=$TOKEN
|
||||
- DB_HOST=database
|
||||
- DB_HOST=db
|
||||
- DB_USERNAME=$DB_USERNAME
|
||||
- DB_PASSWORD=$DB_PASSWORD
|
||||
- DB_DATABASE=$DB_DATABASE
|
||||
database:
|
||||
- MONITOR_URL=$MONITOR_URL
|
||||
db:
|
||||
image: postgres:latest
|
||||
restart: unless-stopped
|
||||
ports:
|
||||
|
5
index.ts
5
index.ts
@ -4,8 +4,12 @@ import swapConsole from "./src/tools/consoleSwapper";
|
||||
config();
|
||||
swapConsole();
|
||||
|
||||
import "./src/tools/startupTime";
|
||||
import "./src/client/init";
|
||||
import "./src/commands";
|
||||
import "./src/events";
|
||||
import "./src/cli";
|
||||
import "./src/service";
|
||||
import client from "./src/client";
|
||||
|
||||
function shutdown() {
|
||||
@ -16,3 +20,4 @@ function shutdown() {
|
||||
|
||||
process.on("SIGINT", shutdown);
|
||||
process.on("SIGHUP", shutdown);
|
||||
process.on("SIGTERM", shutdown);
|
1899
package-lock.json
generated
1899
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
41
package.json
41
package.json
@ -1,18 +1,27 @@
|
||||
{
|
||||
"name": "eu.astrogd.white-leopard",
|
||||
"version": "1.0.0-alpha.1",
|
||||
"version": "1.2.0",
|
||||
"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 .",
|
||||
"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",
|
||||
"migration:show": "node --require ts-node/register ./node_modules/typeorm/cli.js migration:show -d src/data/dataSource.ts",
|
||||
"migration:check": "node --require ts-node/register ./node_modules/typeorm/cli.js migration:generate --check -d src/data/dataSource.ts src/data/migrations/data"
|
||||
"start:rebuild": "npm run build && node --enable-source-maps .",
|
||||
"version:dev": "npm version prerelease --preid dev --no-commit-hooks --no-git-tag-version",
|
||||
"version:patch": "npm version prepatch --preid dev --no-commit-hooks --no-git-tag-version",
|
||||
"version:minor": "npm version preminor --preid dev --no-commit-hooks --no-git-tag-version",
|
||||
"version:major": "npm version premajor --preid dev --no-commit-hooks --no-git-tag-version",
|
||||
"version:release": "npm version patch --no-commit-hooks --no-git-tag-version",
|
||||
"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 -d db && node --enable-source-maps .",
|
||||
"migration:create": "node --require ts-node/register ./node_modules/typeorm/cli.js migration:generate -d src/data/dataSource.migration.ts -p src/data/migrations/data",
|
||||
"migration:run": "node --require ts-node/register ./node_modules/typeorm/cli.js migration:run -d src/data/dataSource.migration.ts",
|
||||
"migration:revert": "node --require ts-node/register ./node_modules/typeorm/cli.js migration:revert -d src/data/dataSource.migration.ts",
|
||||
"migration:show": "node --require ts-node/register ./node_modules/typeorm/cli.js migration:show -d src/data/dataSource.migration.ts",
|
||||
"migration:check": "node --require ts-node/register ./node_modules/typeorm/cli.js migration:generate --check -d src/data/dataSource.migration.ts src/data/migrations/data"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@ -25,17 +34,21 @@
|
||||
},
|
||||
"homepage": "https://github.com/r-Overwatch2/eu.astrogd.white-leopard#readme",
|
||||
"devDependencies": {
|
||||
"@types/fs-extra": "^9.0.13",
|
||||
"@types/node": "^18.11.9",
|
||||
"rimraf": "^3.0.2",
|
||||
"@types/express": "^4.17.14",
|
||||
"@types/fs-extra": "^11.0.2",
|
||||
"@types/node": "^18.17.17",
|
||||
"rimraf": "^5.0.1",
|
||||
"shx": "^0.3.4",
|
||||
"ts-node": "^10.9.1",
|
||||
"typescript": "^4.9.3"
|
||||
"typescript": "^5.2.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"discord.js": "^14.6.0",
|
||||
"@astrogd/eu.astrogd.uptime-kuma-push-monitor": "^1.0.0-dev.3",
|
||||
"discord.js": "^14.13.0",
|
||||
"dotenv": "^16.0.3",
|
||||
"fs-extra": "^10.1.0",
|
||||
"express": "^4.18.2",
|
||||
"fs-extra": "^11.0.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`);
|
||||
}
|
140
src/cli/guild.ts
Normal file
140
src/cli/guild.ts
Normal file
@ -0,0 +1,140 @@
|
||||
import { getGuildSetting } from "../tools/data";
|
||||
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 wordCount = await database.getRepository(Badword).count({
|
||||
where: {
|
||||
guildID: args[1]
|
||||
}
|
||||
});
|
||||
|
||||
console.log(`Guild ${args[1]}:
|
||||
- Preserve Settings: ${settings.preserveDataOnGuildLeave ? "ENABLED" : "DISABLED"}
|
||||
- Logchannel: ${settings.notificationChannelID ? `ENABLED (${settings.notificationChannelID})` : "DISABLED"}
|
||||
- blocked Words: ${wordCount}`);
|
||||
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;
|
||||
}
|
||||
|
||||
case "delete": {
|
||||
if (!args[1]) return printHelp();
|
||||
|
||||
await database.getRepository(GuildSetting).delete({
|
||||
id: args[1]
|
||||
});
|
||||
|
||||
await database.getRepository(Badword).delete({
|
||||
guildID: args[1]
|
||||
});
|
||||
|
||||
console.log(`Deleted all data for guild ${args[1]}`);
|
||||
break;
|
||||
}
|
||||
|
||||
default: {
|
||||
printHelp();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function printHelp() {
|
||||
console.log(`Usage "guild":
|
||||
|
||||
guild info [GUILDID]
|
||||
guild words [get|add|remove|clear]
|
||||
guild delete [GUILDID]`);
|
||||
}
|
||||
|
||||
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]`);
|
||||
}
|
75
src/cli/index.ts
Normal file
75
src/cli/index.ts
Normal file
@ -0,0 +1,75 @@
|
||||
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 startupTime from "../tools/startupTime";
|
||||
import client from "../client";
|
||||
|
||||
import guild from "./guild";
|
||||
import global from "./global";
|
||||
|
||||
const console = new Console(process.stdout);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
case "servercount": {
|
||||
console.log(`This bot is on ${client.guilds.cache.size} guilds`);
|
||||
break;
|
||||
}
|
||||
|
||||
default: {
|
||||
console.log(`Unknown command. Try "help" for help`);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
rl.prompt();
|
||||
});
|
||||
|
||||
function printHelp() {
|
||||
console.log(`Commands:
|
||||
|
||||
version
|
||||
uptime
|
||||
guild
|
||||
global
|
||||
help
|
||||
servercount
|
||||
clear`);
|
||||
}
|
@ -1,10 +1,25 @@
|
||||
import { runCleanup } from "../service";
|
||||
import client from "./index";
|
||||
import { initPromise } from "../data/dataSource";
|
||||
import pushMonitor from "@astrogd/eu.astrogd.uptime-kuma-push-monitor";
|
||||
|
||||
const token = process.env["TOKEN"];
|
||||
if (!token) throw new ReferenceError("TOKEN environment variable is missing");
|
||||
|
||||
async function run() {
|
||||
console.log("Establishing database connection");
|
||||
await initPromise;
|
||||
console.log("Connection established\nAuthenticating with Discord API");
|
||||
client.login(token);
|
||||
}
|
||||
|
||||
client.on("ready", () => {
|
||||
client.on("ready", async () => {
|
||||
console.log(`Connected to Discord API. Bot account is ${client.user?.tag} (${client.user?.id})`);
|
||||
if (process.env["MONITOR_URL"]) pushMonitor.register(process.env["MONITOR_URL"], 120);
|
||||
pushMonitor.enableShutdownNotifications();
|
||||
pushMonitor.setPerformanceHandler(() => client.ws.ping);
|
||||
|
||||
runCleanup();
|
||||
});
|
||||
|
||||
run();
|
180
src/commands/blocklist.ts
Normal file
180
src/commands/blocklist.ts
Normal file
@ -0,0 +1,180 @@
|
||||
import { ChatInputCommandInteraction, PermissionFlagsBits, SlashCommandBuilder } from "discord.js";
|
||||
import { database, Badword } from "../data";
|
||||
import { IsNull } from "typeorm";
|
||||
import { getGuildSetting } from "../tools/data";
|
||||
import getDefaultEmbed, { getFailedEmbed, getSuccessEmbed } from "../tools/defaultEmbeds";
|
||||
import { getGuildChannel } from "../tools/discord";
|
||||
import { Color, Emoji } from "../tools/design";
|
||||
import { execute as showBlocklistRunner } from "./showblocklist";
|
||||
|
||||
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 logChannel = settings.notificationChannelID ? await getGuildChannel(interaction.guildId, settings.notificationChannelID) : null;
|
||||
|
||||
switch (interaction.options.getSubcommand(true)) {
|
||||
case "get": {
|
||||
await showBlocklistRunner(interaction);
|
||||
break;
|
||||
}
|
||||
|
||||
case "add": {
|
||||
const count = await database.getRepository(Badword).count({
|
||||
where: {
|
||||
guildID: interaction.guildId
|
||||
}
|
||||
});
|
||||
|
||||
const limit = 100;
|
||||
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,19 @@
|
||||
import * as notification from "./notification";
|
||||
import * as notification from "./logchannel";
|
||||
import * as blocklist from "./blocklist";
|
||||
import * as info from "./info";
|
||||
import * as preserveSettings from "./preserveSettings";
|
||||
import * as showBlocklist from "./showblocklist";
|
||||
import * as showSettings from "./showSettings";
|
||||
|
||||
const array = [notification.builder.toJSON()];
|
||||
const commands = [];
|
||||
commands.push(notification);
|
||||
commands.push(blocklist);
|
||||
commands.push(info);
|
||||
commands.push(preserveSettings);
|
||||
commands.push(showBlocklist);
|
||||
commands.push(showSettings);
|
||||
|
||||
const array = commands.map((command) => command.builder.toJSON());
|
||||
|
||||
export {
|
||||
array
|
||||
|
@ -1,10 +1,20 @@
|
||||
import { ChatInputCommandInteraction, Collection, Events, SlashCommandBuilder } from "discord.js";
|
||||
import * as notification from "./notification";
|
||||
import * as notification from "./logchannel";
|
||||
import * as blocklist from "./blocklist";
|
||||
import * as info from "./info";
|
||||
import * as preserveSettings from "./preserveSettings";
|
||||
import * as showBlocklist from "./showblocklist";
|
||||
import * as showSettings from "./showSettings";
|
||||
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);
|
||||
commands.set(preserveSettings.builder.name, preserveSettings);
|
||||
commands.set(showBlocklist.builder.name, showBlocklist);
|
||||
commands.set(showSettings.builder.name, showSettings);
|
||||
|
||||
client.on(Events.InteractionCreate, async (interaction) => {
|
||||
if (!interaction.isChatInputCommand()) return;
|
||||
@ -34,6 +44,6 @@ client.on(Events.InteractionCreate, async (interaction) => {
|
||||
await interaction.reply({
|
||||
embeds: [embed],
|
||||
ephemeral: true
|
||||
}).catch();
|
||||
}).catch(() => {});
|
||||
}
|
||||
});
|
59
src/commands/info.ts
Normal file
59
src/commands/info.ts
Normal file
@ -0,0 +1,59 @@
|
||||
import { ChatInputCommandInteraction, SlashCommandBuilder } from "discord.js";
|
||||
import { IsNull } from "typeorm";
|
||||
import { Badword, database } from "../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 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(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: "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,13 +1,15 @@
|
||||
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 { SlashCommandBuilder, PermissionFlagsBits, ChannelType, ChatInputCommandInteraction, NewsChannel, TextBasedChannel, CategoryChannel, StageChannel, TextChannel, PrivateThreadChannel, PublicThreadChannel, VoiceChannel, APIInteractionDataResolvedChannel, ForumChannel } from "discord.js";
|
||||
import getDefaultEmbed, { getFailedEmbed, getSuccessEmbed } from "../tools/defaultEmbeds";
|
||||
import { database, GuildSetting } from "../data";
|
||||
import client from "../client";
|
||||
import { getGuildSetting } from "../tools/data";
|
||||
import { getGuildChannel, getChannelPermission } from "../tools/discord";
|
||||
import { Emoji } from "../tools/design";
|
||||
|
||||
const builder = new SlashCommandBuilder();
|
||||
builder.setName("logchannel");
|
||||
builder.setDescription("Configures the log channel");
|
||||
builder.setDMPermission(false);
|
||||
builder.setDefaultMemberPermissions(PermissionFlagsBits.ManageChannels);
|
||||
builder.setDefaultMemberPermissions(PermissionFlagsBits.ManageGuild);
|
||||
builder.addChannelOption((option) => {
|
||||
option.addChannelTypes(ChannelType.GuildText, ChannelType.GuildAnnouncement);
|
||||
option.setName("channel");
|
||||
@ -16,31 +18,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;
|
||||
|
||||
@ -58,7 +35,7 @@ async function resetNotificationChannel(guildSetting: GuildSetting, interaction:
|
||||
if (logChannel && logChannel.isTextBased()) {
|
||||
logChannel.send({
|
||||
embeds: [logEmbed]
|
||||
}).catch();
|
||||
}).catch(() => {});
|
||||
}
|
||||
|
||||
const embed = getSuccessEmbed();
|
||||
@ -66,7 +43,7 @@ async function resetNotificationChannel(guildSetting: GuildSetting, interaction:
|
||||
interaction.reply({
|
||||
embeds: [embed],
|
||||
ephemeral: true
|
||||
}).catch();
|
||||
}).catch(() => {});
|
||||
}
|
||||
|
||||
const execute = async (interaction: ChatInputCommandInteraction) => {
|
||||
@ -78,10 +55,24 @@ const execute = async (interaction: ChatInputCommandInteraction) => {
|
||||
if (!optionVal) return await resetNotificationChannel(guildSetting, interaction);
|
||||
|
||||
const channel = getTextBasedChannel(optionVal);
|
||||
if (channel.isDMBased()) return;
|
||||
|
||||
const permissions = await getChannelPermission(channel);
|
||||
if (!permissions || !permissions.has(PermissionFlagsBits.ViewChannel) || !permissions.has(PermissionFlagsBits.SendMessages)) {
|
||||
const embed = getFailedEmbed();
|
||||
embed.setDescription(`Bot doesn't have permission to view and/or write in channel <#${channel.id}>`);
|
||||
interaction.reply({
|
||||
embeds: [ embed ],
|
||||
ephemeral: true
|
||||
}).catch(() => {});
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (guildSetting.notificationChannelID) {
|
||||
const oldLogChannel = await getGuildChannel(guildSetting.id, guildSetting.notificationChannelID);
|
||||
const embed = getDefaultEmbed();
|
||||
embed.setTitle("Settings changed");
|
||||
embed.setTitle(`${Emoji.SETTINGS} Settings changed`);
|
||||
embed.setDescription(`Log channel has been switched to <#${channel.id}>`);
|
||||
embed.addFields({
|
||||
name: "This action was performed by",
|
||||
@ -91,7 +82,7 @@ const execute = async (interaction: ChatInputCommandInteraction) => {
|
||||
if (oldLogChannel && oldLogChannel.isTextBased()) {
|
||||
oldLogChannel.send({
|
||||
embeds: [embed]
|
||||
}).catch();
|
||||
}).catch(() => {});
|
||||
}
|
||||
}
|
||||
|
||||
@ -99,7 +90,7 @@ const execute = async (interaction: ChatInputCommandInteraction) => {
|
||||
await database.getRepository(GuildSetting).save(guildSetting);
|
||||
|
||||
const embed = getDefaultEmbed();
|
||||
embed.setTitle("Settings changed");
|
||||
embed.setTitle(`${Emoji.SETTINGS} Settings changed`);
|
||||
embed.setDescription("This channel has been set as the log channel");
|
||||
embed.addFields({
|
||||
name: "This action was performed by",
|
||||
@ -108,7 +99,7 @@ const execute = async (interaction: ChatInputCommandInteraction) => {
|
||||
|
||||
channel.send({
|
||||
embeds: [embed]
|
||||
}).catch();
|
||||
}).catch(() => {});
|
||||
|
||||
const reply = getSuccessEmbed();
|
||||
reply.setDescription(`Log channel was set to <#${channel.id}>`);
|
||||
@ -116,7 +107,7 @@ const execute = async (interaction: ChatInputCommandInteraction) => {
|
||||
interaction.reply({
|
||||
embeds: [reply],
|
||||
ephemeral: true
|
||||
}).catch();
|
||||
}).catch(() => {});
|
||||
|
||||
return;
|
||||
}
|
66
src/commands/preserveSettings.ts
Normal file
66
src/commands/preserveSettings.ts
Normal file
@ -0,0 +1,66 @@
|
||||
import { SlashCommandBuilder, ChatInputCommandInteraction, PermissionFlagsBits } from "discord.js";
|
||||
import { database, GuildSetting } from "../data";
|
||||
import { getGuildSetting } from "../tools/data";
|
||||
import getDefaultEmbed, { getSuccessEmbed } from "../tools/defaultEmbeds";
|
||||
import { Emoji } from "../tools/design";
|
||||
import { getGuildChannel } from "../tools/discord";
|
||||
|
||||
const builder = new SlashCommandBuilder();
|
||||
builder.setName("preservesettings");
|
||||
builder.setDescription("Sets if the bot should save the server settings and blocklist if it leaves the server or delete it");
|
||||
builder.addStringOption((option) => {
|
||||
option.addChoices({
|
||||
name: "Keep data when bot leaves the server",
|
||||
value: "keep"
|
||||
}, {
|
||||
name: "Delete data when bot leaves the server",
|
||||
value: "delete"
|
||||
});
|
||||
option.setName("behaviour");
|
||||
option.setDescription("How the bot behaves when leaving the server");
|
||||
option.setRequired(true);
|
||||
return option;
|
||||
});
|
||||
builder.setDMPermission(false);
|
||||
builder.setDefaultMemberPermissions(PermissionFlagsBits.Administrator);
|
||||
|
||||
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 option = interaction.options.getString("behaviour", true).toLowerCase();
|
||||
|
||||
if (option !== "keep" && option !== "delete") throw new TypeError(`option "behaviour" expected to be of type "keep" | "delete" but was "${option}"`);
|
||||
|
||||
settings.preserveDataOnGuildLeave = option === "keep";
|
||||
|
||||
await database.getRepository(GuildSetting).save(settings);
|
||||
|
||||
const embed = getSuccessEmbed();
|
||||
embed.setDescription(`Preserve settings on server leave is now ${settings.preserveDataOnGuildLeave ? "ENABLED" : "DISABLED"}`);
|
||||
|
||||
interaction.reply({
|
||||
embeds: [embed],
|
||||
ephemeral: true
|
||||
}).catch(() => {});
|
||||
|
||||
if (!settings.notificationChannelID) return;
|
||||
const logChannel = await getGuildChannel(interaction.guildId, settings.notificationChannelID);
|
||||
if (!logChannel || !logChannel.isTextBased()) return;
|
||||
const logEmbed = getDefaultEmbed();
|
||||
logEmbed.setTitle(`${Emoji.SETTINGS} Settings changed`);
|
||||
logEmbed.setDescription(`Preserve settings on server leave is now ${settings.preserveDataOnGuildLeave ? "ENABLED" : "DISABLED"}`);
|
||||
logEmbed.addFields({
|
||||
name: "This action was performed by",
|
||||
value: `${interaction.user.tag} (${interaction.user.id})`
|
||||
});
|
||||
|
||||
logChannel.send({
|
||||
embeds: [logEmbed]
|
||||
}).catch(() => {});
|
||||
}
|
||||
|
||||
export {
|
||||
builder,
|
||||
execute
|
||||
}
|
110
src/commands/showSettings.ts
Normal file
110
src/commands/showSettings.ts
Normal file
@ -0,0 +1,110 @@
|
||||
import { ChatInputCommandInteraction, Guild, PermissionFlagsBits, SlashCommandBuilder } from "discord.js";
|
||||
import client from "../client";
|
||||
import { Badword, database } from "../data";
|
||||
import { getGuildSetting } from "../tools/data";
|
||||
import getDefaultEmbed from "../tools/defaultEmbeds";
|
||||
import { Color, Emoji } from "../tools/design";
|
||||
import { getGuildChannel, getChannelPermission } from "../tools/discord";
|
||||
|
||||
const builder = new SlashCommandBuilder();
|
||||
builder.setName("showsettings");
|
||||
builder.setDescription("Show the current settings of this guild");
|
||||
builder.setDMPermission(false);
|
||||
builder.setDefaultMemberPermissions(PermissionFlagsBits.ManageGuild);
|
||||
|
||||
async function execute(interaction: ChatInputCommandInteraction): Promise<void> {
|
||||
if (!interaction.inGuild()) throw new Error("Interaction was performed outside guild context");
|
||||
|
||||
const guild = await new Promise<null | Guild>((resolve) => {
|
||||
client.guilds.fetch(interaction.guildId).catch(() => {resolve(null)}).then((guild) => {resolve(guild || null)});
|
||||
});
|
||||
|
||||
if (!guild) throw new Error("Guild is unavailable");
|
||||
|
||||
const settings = await getGuildSetting(interaction.guildId);
|
||||
const wordCount = await database.getRepository(Badword).count({
|
||||
where: {
|
||||
guildID: interaction.guildId
|
||||
}
|
||||
});
|
||||
|
||||
const logChannel = settings.notificationChannelID ? await getGuildChannel(interaction.guildId, settings.notificationChannelID) : null;
|
||||
|
||||
const embed = getDefaultEmbed();
|
||||
embed.setTitle(`Settings from guild ${interaction.guild?.name || ""} (${interaction.guildId})`);
|
||||
embed.setDescription("Thanks for using this bot");
|
||||
embed.setColor(Color.INFORMING_BLUE);
|
||||
embed.addFields({
|
||||
name: "Logchannel",
|
||||
value: settings.notificationChannelID ? `<#${settings.notificationChannelID}>` : "Not configured",
|
||||
inline: true
|
||||
}, {
|
||||
name: "Words in Blocklist",
|
||||
value: `${wordCount}/100`,
|
||||
inline: true
|
||||
}, {
|
||||
name: `Preserve data on server leave is ${settings.preserveDataOnGuildLeave ? "active" : "inactive"}`,
|
||||
value: settings.preserveDataOnGuildLeave ? "Your settings will be saved even if the bot gets kicked" : "Your settings will be deleted as soon as the bot leaves this server"
|
||||
}, {
|
||||
name: `${Emoji.WAVING} Found a bug? Want to request a feature? Say hello?`,
|
||||
value: "Join the support server at https://go.astrogd.eu/discord"
|
||||
});
|
||||
|
||||
const embeds = [];
|
||||
embeds.push(embed);
|
||||
|
||||
const invalidLogChannelEmbed = getDefaultEmbed();
|
||||
invalidLogChannelEmbed.setColor(Color.WARNING_YELLOW);
|
||||
invalidLogChannelEmbed.setTitle(`${Emoji.CHAT_DENY} Logchannel is misconfigured!`);
|
||||
invalidLogChannelEmbed.setDescription("The bot is unable to read and/or write in the configured logChannel. Please adjust your permissions.");
|
||||
|
||||
const missingPermissionEmbed = getDefaultEmbed();
|
||||
missingPermissionEmbed.setColor(Color.WARNING_YELLOW);
|
||||
missingPermissionEmbed.setTitle(`${Emoji.CHAT_DENY} Bot is missing permissions to function properly!`);
|
||||
missingPermissionEmbed.setDescription("Without these missing Permissions, the bot won't work properly and might lead to unexpected behavior");
|
||||
|
||||
if (logChannel && logChannel.isTextBased()) {
|
||||
const permissions = await getChannelPermission(logChannel);
|
||||
if (!permissions || !permissions.has(PermissionFlagsBits.ViewChannel) || !permissions.has(PermissionFlagsBits.SendMessages)) {
|
||||
embeds.push(invalidLogChannelEmbed);
|
||||
}
|
||||
}
|
||||
|
||||
if ((!logChannel || !logChannel.isTextBased()) && settings.notificationChannelID) {
|
||||
embeds.push(invalidLogChannelEmbed);
|
||||
}
|
||||
|
||||
const member = await guild.members.fetchMe();
|
||||
if (!member.permissions.has(PermissionFlagsBits.ViewAuditLog)) {
|
||||
missingPermissionEmbed.addFields({
|
||||
name: "View Audit Logs",
|
||||
value: "With this permission the bot can determine who created or updated a channel with a blocked word"
|
||||
});
|
||||
}
|
||||
|
||||
if (!member.permissions.has(PermissionFlagsBits.ViewChannel)) {
|
||||
missingPermissionEmbed.addFields({
|
||||
name: "View Channels",
|
||||
value: "If the bot can't see a channel, it won't be able to detect blocked words in it"
|
||||
});
|
||||
}
|
||||
|
||||
if (!member.permissions.has(PermissionFlagsBits.ManageChannels)) {
|
||||
missingPermissionEmbed.addFields({
|
||||
name: "Manage Channels",
|
||||
value: "If the bot can't delete a channel, it can't protect your server from channels with blocked words"
|
||||
});
|
||||
}
|
||||
|
||||
if (missingPermissionEmbed.data.fields?.length || 0 > 0) embeds.push(missingPermissionEmbed);
|
||||
|
||||
interaction.reply({
|
||||
embeds: embeds,
|
||||
ephemeral: true
|
||||
}).catch(() => {});
|
||||
}
|
||||
|
||||
export {
|
||||
builder,
|
||||
execute
|
||||
}
|
37
src/commands/showblocklist.ts
Normal file
37
src/commands/showblocklist.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import { ChatInputCommandInteraction, PermissionFlagsBits, SlashCommandBuilder } from "discord.js";
|
||||
import { IsNull } from "typeorm";
|
||||
import { Badword, database } from "../data";
|
||||
|
||||
const builder = new SlashCommandBuilder();
|
||||
builder.setName("showblocklist");
|
||||
builder.setDescription("Shows the blocklist of this server");
|
||||
builder.setDMPermission(false);
|
||||
builder.setDefaultMemberPermissions(PermissionFlagsBits.ManageChannels);
|
||||
|
||||
async function execute(interaction: ChatInputCommandInteraction): Promise<void> {
|
||||
if (!interaction.inGuild()) throw new Error("Command was executed outside guild context");
|
||||
|
||||
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}/100)\`\`\`\n||${guildBadWords.map((word) => word.value).reduce((prev, next) => prev + ", " + next, "").slice(2)} ||`,
|
||||
ephemeral: true
|
||||
}).catch(() => {});
|
||||
}
|
||||
|
||||
export {
|
||||
builder,
|
||||
execute
|
||||
}
|
3
src/data/dataSource.migration.ts
Normal file
3
src/data/dataSource.migration.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import dataSource from "./dataSource";
|
||||
|
||||
export default dataSource;
|
@ -28,6 +28,9 @@ const dataSource = new DataSource({
|
||||
migrationsTransactionMode: "each"
|
||||
});
|
||||
|
||||
dataSource.initialize();
|
||||
const initPromise = dataSource.initialize();
|
||||
|
||||
export default dataSource;
|
||||
export {
|
||||
initPromise
|
||||
}
|
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
|
||||
`);
|
||||
}
|
||||
|
||||
}
|
19
src/data/migrations/1669392941776-data.ts
Normal file
19
src/data/migrations/1669392941776-data.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { MigrationInterface, QueryRunner } from "typeorm";
|
||||
|
||||
export class data1669392941776 implements MigrationInterface {
|
||||
name = 'data1669392941776'
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`
|
||||
ALTER TABLE "guild_setting"
|
||||
ADD "preserveDataOnGuildLeave" boolean NOT NULL DEFAULT false
|
||||
`);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`
|
||||
ALTER TABLE "guild_setting" DROP COLUMN "preserveDataOnGuildLeave"
|
||||
`);
|
||||
}
|
||||
|
||||
}
|
19
src/data/migrations/1669686263307-data.ts
Normal file
19
src/data/migrations/1669686263307-data.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { MigrationInterface, QueryRunner } from "typeorm";
|
||||
|
||||
export class data1669686263307 implements MigrationInterface {
|
||||
name = 'data1669686263307'
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`
|
||||
ALTER TABLE "guild_setting" DROP COLUMN "isPremiumUntil"
|
||||
`);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`
|
||||
ALTER TABLE "guild_setting"
|
||||
ADD "isPremiumUntil" TIMESTAMP
|
||||
`);
|
||||
}
|
||||
|
||||
}
|
@ -8,6 +8,6 @@ export class GuildSetting {
|
||||
@Column("varchar", { nullable: true, default: null })
|
||||
notificationChannelID!: string | null;
|
||||
|
||||
@Column("date", { nullable: true, default: null })
|
||||
isPremiumUntil!: Date | null;
|
||||
@Column("boolean", { default: false })
|
||||
preserveDataOnGuildLeave!: boolean
|
||||
}
|
116
src/events/channelCreate.ts
Normal file
116
src/events/channelCreate.ts
Normal file
@ -0,0 +1,116 @@
|
||||
import client from "../client";
|
||||
import { AuditLogEvent, Events, GuildAuditLogsEntry, PermissionFlagsBits, User } from "discord.js";
|
||||
import { getGuildSetting } from "../tools/data";
|
||||
import { Badword, database } from "../data";
|
||||
import { IsNull } from "typeorm";
|
||||
import getDefaultEmbed, { getFailedEmbed, getUserReportEmbed } from "../tools/defaultEmbeds";
|
||||
import { getGuildChannel } from "../tools/discord";
|
||||
import { Color, Emoji } from "../tools/design";
|
||||
|
||||
client.on(Events.ChannelCreate, async (newChannel) => {
|
||||
if (newChannel.isDMBased()) return;
|
||||
const name = newChannel.name.toLowerCase();
|
||||
|
||||
const guild = newChannel.guild;
|
||||
const settings = await getGuildSetting(guild.id);
|
||||
|
||||
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;
|
||||
|
||||
let responsibleUser: User | null;
|
||||
|
||||
try {
|
||||
const clientMember = await guild.members.fetchMe();
|
||||
const canSeeAuditLog = clientMember.permissions.has(PermissionFlagsBits.ViewAuditLog);
|
||||
const auditLogs = canSeeAuditLog ? await guild.fetchAuditLogs({
|
||||
type: AuditLogEvent.ChannelCreate,
|
||||
limit: 50
|
||||
}) : undefined;
|
||||
|
||||
const change = auditLogs?.entries.filter((entry) => {
|
||||
if (entry.target.id !== newChannel.id) return false;
|
||||
|
||||
return entry.changes.filter((change) => {
|
||||
return change.key === "name" && change.new === newChannel.name;
|
||||
}).length > 0;
|
||||
}).reduce<null | GuildAuditLogsEntry<AuditLogEvent.ChannelCreate, "Create", "Channel", AuditLogEvent.ChannelCreate>>((unknown, curr) => {
|
||||
if (!unknown) return curr;
|
||||
const prev = unknown as GuildAuditLogsEntry<AuditLogEvent.ChannelCreate, "Create", "Channel", AuditLogEvent.ChannelCreate>
|
||||
return curr.createdTimestamp > prev.createdTimestamp ? curr : prev;
|
||||
}, null);
|
||||
|
||||
responsibleUser = change?.executor || null;
|
||||
} catch (error) {
|
||||
responsibleUser = null;
|
||||
}
|
||||
|
||||
const logChannel = settings.notificationChannelID ? await getGuildChannel(guild.id, settings.notificationChannelID) : null;
|
||||
|
||||
try {
|
||||
if (!newChannel.deletable) throw new Error("Missing permissions to delete channel");
|
||||
await newChannel.delete("[Automated] Detected blocked word in channel name");
|
||||
} catch (error) {
|
||||
if (!logChannel || !logChannel.isTextBased()) return;
|
||||
const embed = getFailedEmbed();
|
||||
embed.setDescription(`Couldn't delete <#${newChannel.id}> (${newChannel.id}): ${error instanceof Error ? error.message : error}`);
|
||||
embed.addFields({
|
||||
name: "Detected banned word:",
|
||||
value: `||${found}||`
|
||||
}, {
|
||||
name: "Channel created by:",
|
||||
value: responsibleUser ? `${responsibleUser.tag} (${responsibleUser.id})` : "`Couldn't detect responsible user :(`"
|
||||
});
|
||||
|
||||
logChannel.send({
|
||||
embeds: [embed]
|
||||
}).catch(() => {});
|
||||
return;
|
||||
}
|
||||
|
||||
if (responsibleUser) {
|
||||
const embed = getUserReportEmbed(guild.name, newChannel.name);
|
||||
responsibleUser.send({
|
||||
embeds: [embed]
|
||||
}).catch(() => {});
|
||||
}
|
||||
|
||||
if (!logChannel || !logChannel.isTextBased()) return;
|
||||
const embed = getDefaultEmbed();
|
||||
embed.setTitle(`${Emoji.SECURITY_CHALLENGE_FAILED} Blocked word detected`);
|
||||
embed.setDescription(`||#${newChannel.name}|| (${newChannel.id}) has been deleted because its name contained a blocked word.`);
|
||||
embed.setColor(Color.WARNING_YELLOW);
|
||||
embed.addFields({
|
||||
name: "Detected banned word:",
|
||||
value: `||${found}||`,
|
||||
inline: true
|
||||
}, {
|
||||
name: "Channel created by:",
|
||||
value: responsibleUser ? `${responsibleUser.tag} (${responsibleUser.id})` : "`Couldn't detect responsible user :(`"
|
||||
});
|
||||
|
||||
logChannel.send({
|
||||
embeds: [embed]
|
||||
}).catch(() => {});
|
||||
});
|
116
src/events/channelUpdate.ts
Normal file
116
src/events/channelUpdate.ts
Normal file
@ -0,0 +1,116 @@
|
||||
import client from "../client";
|
||||
import { AuditLogEvent, Events, GuildAuditLogsEntry, PermissionFlagsBits, User } from "discord.js";
|
||||
import { getGuildSetting } from "../tools/data";
|
||||
import { Badword, database } from "../data";
|
||||
import { IsNull } from "typeorm";
|
||||
import getDefaultEmbed, { getFailedEmbed, getUserReportEmbed } 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();
|
||||
|
||||
const guild = oldChannel.guild;
|
||||
const settings = await getGuildSetting(guild.id);
|
||||
|
||||
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;
|
||||
|
||||
let responsibleUser: User | null;
|
||||
|
||||
try {
|
||||
const clientMember = await guild.members.fetchMe();
|
||||
const canSeeAuditLog = clientMember.permissions.has(PermissionFlagsBits.ViewAuditLog);
|
||||
const auditLogs = canSeeAuditLog ? await guild.fetchAuditLogs({
|
||||
type: AuditLogEvent.ChannelUpdate,
|
||||
limit: 50
|
||||
}) : undefined;
|
||||
|
||||
const change = auditLogs?.entries.filter((entry) => {
|
||||
if (entry.target.id !== newChannel.id) return false;
|
||||
|
||||
return entry.changes.filter((change) => {
|
||||
return change.key === "name" && change.new === newChannel.name;
|
||||
}).length > 0;
|
||||
}).reduce<null | GuildAuditLogsEntry<AuditLogEvent.ChannelUpdate, "Update", "Channel", AuditLogEvent.ChannelUpdate>>((unknown, curr) => {
|
||||
if (!unknown) return curr;
|
||||
const prev = unknown as GuildAuditLogsEntry<AuditLogEvent.ChannelUpdate, "Update", "Channel", AuditLogEvent.ChannelUpdate>
|
||||
return curr.createdTimestamp > prev.createdTimestamp ? curr : prev;
|
||||
}, null);
|
||||
|
||||
responsibleUser = change?.executor || null;
|
||||
} catch (error) {
|
||||
responsibleUser = null;
|
||||
}
|
||||
|
||||
const logChannel = settings.notificationChannelID ? await getGuildChannel(guild.id, settings.notificationChannelID) : null;
|
||||
|
||||
try {
|
||||
if (!newChannel.deletable) throw new Error("Missing permissions to delete channel");
|
||||
await newChannel.delete("[Automated] Detected blocked word in channel name");
|
||||
} catch (error) {
|
||||
if (!logChannel || !logChannel.isTextBased()) return;
|
||||
const embed = getFailedEmbed();
|
||||
embed.setDescription(`Couldn't delete <#${newChannel.id}> (${newChannel.id}): ${error instanceof Error ? error.message : error}`);
|
||||
embed.addFields({
|
||||
name: "Detected banned word:",
|
||||
value: `||${found}||`
|
||||
}, {
|
||||
name: "Channel renamed by:",
|
||||
value: responsibleUser ? `${responsibleUser.tag} (${responsibleUser.id})` : "`Couldn't detect responsible user :(`"
|
||||
});
|
||||
|
||||
logChannel.send({
|
||||
embeds: [embed]
|
||||
}).catch(() => {});
|
||||
return;
|
||||
}
|
||||
|
||||
if (responsibleUser) {
|
||||
const embed = getUserReportEmbed(guild.name, newChannel.name);
|
||||
responsibleUser.send({
|
||||
embeds: [embed]
|
||||
}).catch(() => {});
|
||||
}
|
||||
|
||||
if (!logChannel || !logChannel.isTextBased()) return;
|
||||
const embed = getDefaultEmbed();
|
||||
embed.setTitle(`${Emoji.SECURITY_CHALLENGE_FAILED} Blocked word detected`);
|
||||
embed.setDescription(`||#${newChannel.name}|| (${newChannel.id}) has been deleted because its name contained a blocked word.`);
|
||||
embed.setColor(Color.WARNING_YELLOW);
|
||||
embed.addFields({
|
||||
name: "Detected banned word:",
|
||||
value: `||${found}||`,
|
||||
inline: true
|
||||
}, {
|
||||
name: "Channel renamed by:",
|
||||
value: responsibleUser ? `${responsibleUser.tag} (${responsibleUser.id})` : "`Couldn't detect responsible user :(`"
|
||||
});
|
||||
|
||||
logChannel.send({
|
||||
embeds: [embed]
|
||||
}).catch(() => {});
|
||||
});
|
17
src/events/guildDelete.ts
Normal file
17
src/events/guildDelete.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import client from "../client";
|
||||
import { Events } from "discord.js";
|
||||
import { getGuildSetting } from "../tools/data";
|
||||
import { Badword, database, GuildSetting } from "../data";
|
||||
|
||||
client.on(Events.GuildDelete, async (guild) => {
|
||||
const settings = await getGuildSetting(guild.id);
|
||||
if (settings.preserveDataOnGuildLeave) return;
|
||||
|
||||
await database.getRepository(GuildSetting).delete({
|
||||
id: guild.id
|
||||
});
|
||||
|
||||
await database.getRepository(Badword).delete({
|
||||
guildID: guild.id
|
||||
});
|
||||
});
|
3
src/events/index.ts
Normal file
3
src/events/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import "./channelUpdate";
|
||||
import "./guildDelete";
|
||||
import "./channelCreate";
|
39
src/service/cleanup.ts
Normal file
39
src/service/cleanup.ts
Normal file
@ -0,0 +1,39 @@
|
||||
import { Badword, database, GuildSetting } from "../data";
|
||||
import client from "../client";
|
||||
import { DiscordAPIError } from "discord.js";
|
||||
|
||||
export default async function execute(): Promise<void> {
|
||||
const guilds = await database.getRepository(GuildSetting).find({
|
||||
where: {
|
||||
preserveDataOnGuildLeave: false
|
||||
}
|
||||
});
|
||||
|
||||
for (const guild of guilds) {
|
||||
try {
|
||||
await client.guilds.fetch(guild.id);
|
||||
} catch (error) {
|
||||
if (!(error instanceof DiscordAPIError)) {
|
||||
console.error(`service.cleanup failed: ${error}`);
|
||||
return;
|
||||
}
|
||||
|
||||
const id = guild.id;
|
||||
|
||||
// 5001 = Missing access
|
||||
if (error.code.toString() !== "50001") {
|
||||
console.warn(`Guild ${guild.id} is unavailable but not because of error 5001:\n${error}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
await database.getRepository(Badword).delete({
|
||||
guildID: guild.id
|
||||
});
|
||||
|
||||
await database.getRepository(GuildSetting).remove(guild);
|
||||
console.log(`Removed data for guild ${id}`);
|
||||
}
|
||||
}
|
||||
|
||||
console.log("Cleanup completed");
|
||||
}
|
5
src/service/index.ts
Normal file
5
src/service/index.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import runCleanup from "./cleanup";
|
||||
|
||||
export {
|
||||
runCleanup
|
||||
}
|
18
src/tools/data.ts
Normal file
18
src/tools/data.ts
Normal file
@ -0,0 +1,18 @@
|
||||
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.notificationChannelID = null;
|
||||
guildSetting.preserveDataOnGuildLeave = false;
|
||||
}
|
||||
|
||||
return guildSetting;
|
||||
}
|
@ -1,24 +1,43 @@
|
||||
import pack from "../../package.json";
|
||||
import { EmbedBuilder } from "discord.js";
|
||||
import client from "../client";
|
||||
import { Color, Emoji } from "./design";
|
||||
|
||||
// const _coolColors = [0x054566];
|
||||
|
||||
export default function getDefaultEmbed(): EmbedBuilder {
|
||||
const embed = new EmbedBuilder();
|
||||
embed.setFooter({
|
||||
text: `Channel filter V${pack.version} by AstroGD®`,
|
||||
text: `Channel filter V${pack.version}`,
|
||||
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;
|
||||
}
|
||||
|
||||
export function getUserReportEmbed(guildName: string, channelName: string): EmbedBuilder {
|
||||
const embed = getDefaultEmbed();
|
||||
embed.setColor(Color.STOPSIGN_RED);
|
||||
embed.setTitle(`${Emoji.SECURITY_CHALLENGE_FAILED} A channel you modified has been deleted`);
|
||||
embed.setDescription(`Hey there ${Emoji.WAVING}
|
||||
Your channel (#${channelName}) on the "${guildName}" server contained a blacklisted word and was deleted automatically.
|
||||
Please make sure to keep channel names friendly for everyone!`);
|
||||
|
||||
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
|
||||
}
|
25
src/tools/discord.ts
Normal file
25
src/tools/discord.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { GuildBasedChannel, GuildTextBasedChannel, PermissionsBitField } from "discord.js";
|
||||
import client from "../client";
|
||||
|
||||
export async function getGuildChannel(guildID: string, channelID: string): Promise<GuildBasedChannel | null> {
|
||||
try {
|
||||
const guild = await client.guilds.fetch(guildID);
|
||||
if (!guild) return null;
|
||||
|
||||
const channel = await guild.channels.fetch(channelID);
|
||||
return channel;
|
||||
} catch (_error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getChannelPermission(channel: GuildTextBasedChannel): Promise<Readonly<PermissionsBitField> | null> {
|
||||
try {
|
||||
const guildMember = await channel.guild.members.fetchMe();
|
||||
if (!guildMember) return null;
|
||||
|
||||
return channel.permissionsFor(guildMember);
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
3
src/tools/startupTime.ts
Normal file
3
src/tools/startupTime.ts
Normal file
@ -0,0 +1,3 @@
|
||||
const startupTime = new Date();
|
||||
|
||||
export default startupTime;
|
Loading…
x
Reference in New Issue
Block a user