Merge pull request #13 from r-Overwatch2/feature/12-show-warning-if-logchannel-is-unreachable

closes https://github.com/r-Overwatch2/eu.astrogd.white-leopard/issues/12
This commit is contained in:
Lukas | AstroGD 2022-11-29 03:07:01 +01:00 committed by GitHub
commit 781d2e547e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 339 additions and 197 deletions

117
package-lock.json generated
View File

@ -1,12 +1,12 @@
{
"name": "eu.astrogd.white-leopard",
"version": "1.0.0-beta.1",
"version": "1.0.0-beta.3",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "eu.astrogd.white-leopard",
"version": "1.0.0-beta.1",
"version": "1.0.0-beta.3",
"license": "CC-BY-NC-ND-4.0",
"dependencies": {
"discord.js": "^14.6.0",
@ -124,9 +124,9 @@
}
},
"node_modules/@sapphire/shapeshift": {
"version": "3.7.0",
"resolved": "https://registry.npmjs.org/@sapphire/shapeshift/-/shapeshift-3.7.0.tgz",
"integrity": "sha512-A6vI1zJoxhjWo4grsxpBRBgk96SqSdjLX5WlzKp9H+bJbkM07mvwcbtbVAmUZHbi/OG3HLfiZ1rlw4BhH6tsBQ==",
"version": "3.7.1",
"resolved": "https://registry.npmjs.org/@sapphire/shapeshift/-/shapeshift-3.7.1.tgz",
"integrity": "sha512-JmYN/0GW49Vl8Hi4PwrsDBNjcuCylH78vWYolVys74LRIzilAAMINxx4RHQOdvYoz+ceJKVp4+zBbQ5kuIFOLw==",
"dependencies": {
"fast-deep-equal": "^3.1.3",
"lodash.uniqwith": "^4.5.0"
@ -402,19 +402,6 @@
"npm": "1.2.8000 || >= 1.4.16"
}
},
"node_modules/body-parser/node_modules/debug": {
"version": "2.6.9",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
"integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
"dependencies": {
"ms": "2.0.0"
}
},
"node_modules/body-parser/node_modules/ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A=="
},
"node_modules/brace-expansion": {
"version": "1.1.11",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
@ -641,19 +628,11 @@
}
},
"node_modules/debug": {
"version": "4.3.4",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
"integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==",
"version": "2.6.9",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
"integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
"dependencies": {
"ms": "2.1.2"
},
"engines": {
"node": ">=6.0"
},
"peerDependenciesMeta": {
"supports-color": {
"optional": true
}
"ms": "2.0.0"
}
},
"node_modules/depd": {
@ -683,9 +662,9 @@
}
},
"node_modules/discord-api-types": {
"version": "0.37.19",
"resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.37.19.tgz",
"integrity": "sha512-5dVUYJfxCh/RU/S4D7osG3H+Ntl0GBkANO6oVRl35Eo+cd/peoNUAmcSzLzQ9Fqu2RJ9+2vd7DVcqNqYjX38wQ=="
"version": "0.37.20",
"resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.37.20.tgz",
"integrity": "sha512-uAO+55E11rMkYR36/paE1vKN8c2bZa1mgrIaiQIBgIZRKZTDIGOZB+8I5eMRPFJcGxrg16riUu+0aTu2JQEPew=="
},
"node_modules/discord.js": {
"version": "14.6.0",
@ -797,19 +776,6 @@
"node": ">= 0.10.0"
}
},
"node_modules/express/node_modules/debug": {
"version": "2.6.9",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
"integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
"dependencies": {
"ms": "2.0.0"
}
},
"node_modules/express/node_modules/ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A=="
},
"node_modules/fast-deep-equal": {
"version": "3.1.3",
"resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz",
@ -848,19 +814,6 @@
"node": ">= 0.8"
}
},
"node_modules/finalhandler/node_modules/debug": {
"version": "2.6.9",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
"integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
"dependencies": {
"ms": "2.0.0"
}
},
"node_modules/finalhandler/node_modules/ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A=="
},
"node_modules/forwarded": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz",
@ -1208,9 +1161,9 @@
}
},
"node_modules/ms": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A=="
},
"node_modules/mz": {
"version": "2.7.0",
@ -1621,19 +1574,6 @@
"node": ">= 0.8.0"
}
},
"node_modules/send/node_modules/debug": {
"version": "2.6.9",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
"integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
"dependencies": {
"ms": "2.0.0"
}
},
"node_modules/send/node_modules/debug/node_modules/ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A=="
},
"node_modules/send/node_modules/ms": {
"version": "2.1.3",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
@ -2026,6 +1966,27 @@
}
}
},
"node_modules/typeorm/node_modules/debug": {
"version": "4.3.4",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
"integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==",
"dependencies": {
"ms": "2.1.2"
},
"engines": {
"node": ">=6.0"
},
"peerDependenciesMeta": {
"supports-color": {
"optional": true
}
}
},
"node_modules/typeorm/node_modules/ms": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
},
"node_modules/typescript": {
"version": "4.9.3",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.3.tgz",
@ -2040,9 +2001,9 @@
}
},
"node_modules/undici": {
"version": "5.12.0",
"resolved": "https://registry.npmjs.org/undici/-/undici-5.12.0.tgz",
"integrity": "sha512-zMLamCG62PGjd9HHMpo05bSLvvwWOZgGeiWlN/vlqu3+lRo3elxktVGEyLMX+IO7c2eflLjcW74AlkhEZm15mg==",
"version": "5.13.0",
"resolved": "https://registry.npmjs.org/undici/-/undici-5.13.0.tgz",
"integrity": "sha512-UDZKtwb2k7KRsK4SdXWG7ErXiL7yTGgLWvk2AXO1JMjgjh404nFo6tWSCM2xMpJwMPx3J8i/vfqEh1zOqvj82Q==",
"dependencies": {
"busboy": "^1.6.0"
},

View File

@ -6,6 +6,7 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsc && shx cp package-lock.json build/package-lock.json",
"start:rebuild": "npm run build && 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",

View File

@ -1,5 +1,4 @@
import { getGuildSetting, isPremiumActive } from "../tools/data";
import moment from "moment";
import { getGuildSetting } from "../tools/data";
import { Badword, database, GuildSetting } from "../data";
import { Console } from "console";
@ -13,7 +12,6 @@ export default async function execute(args: string[]) {
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]
@ -21,36 +19,12 @@ export default async function execute(args: string[]) {
});
console.log(`Guild ${args[1]}:
- Premium: ${isPremium ? `ACTIVE for ${moment(settings.isPremiumUntil).fromNow(true)}` : "INACTIVE"}
- Preserve Settings: ${settings.preserveDataOnGuildLeave ? "ENABLED" : "DISABLED"}
- 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();
@ -152,7 +126,6 @@ function printHelp() {
console.log(`Usage "guild":
guild info [GUILDID]
guild setPremium [GUILDID] [YYYY-MM-DD or NULL]
guild words [get|add|remove|clear]
guild delete [GUILDID]`);
}

View File

@ -1,7 +1,7 @@
import { ChatInputCommandInteraction, PermissionFlagsBits, SlashCommandBuilder } from "discord.js";
import { database, Badword } from "../data";
import { IsNull } from "typeorm";
import { getGuildSetting, isPremiumActive } from "../tools/data";
import { getGuildSetting } from "../tools/data";
import getDefaultEmbed, { getFailedEmbed, getSuccessEmbed } from "../tools/defaultEmbeds";
import { getGuildChannel } from "../tools/discord";
import { Color, Emoji } from "../tools/design";
@ -46,7 +46,6 @@ 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;
@ -63,14 +62,14 @@ async function execute(interaction: ChatInputCommandInteraction): Promise<void>
}
});
const limit = isPremium ? 100 : 10;
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();
}).catch(() => {});
return;
}
@ -89,7 +88,7 @@ async function execute(interaction: ChatInputCommandInteraction): Promise<void>
interaction.reply({
embeds: [embed],
ephemeral: true
}).catch();
}).catch(() => {});
return;
}
@ -117,7 +116,7 @@ async function execute(interaction: ChatInputCommandInteraction): Promise<void>
if (logChannel && logChannel.isTextBased()) {
logChannel.send({
embeds: [logMessage]
}).catch();
}).catch(() => {});
}
break;
}
@ -138,7 +137,7 @@ async function execute(interaction: ChatInputCommandInteraction): Promise<void>
interaction.reply({
embeds: [embed],
ephemeral: true
}).catch();
}).catch(() => {});
return;
}
@ -151,7 +150,7 @@ async function execute(interaction: ChatInputCommandInteraction): Promise<void>
interaction.reply({
embeds: [embed],
ephemeral: true
}).catch();
}).catch(() => {});
const logMessage = getDefaultEmbed();
logMessage.setTitle(`${Emoji.SETTINGS} Word removed`);
@ -164,7 +163,7 @@ async function execute(interaction: ChatInputCommandInteraction): Promise<void>
if (logChannel && logChannel.isTextBased()) {
logChannel.send({
embeds: [logMessage]
}).catch();
}).catch(() => {});
}
break;
}

View File

@ -44,6 +44,6 @@ client.on(Events.InteractionCreate, async (interaction) => {
await interaction.reply({
embeds: [embed],
ephemeral: true
}).catch();
}).catch(() => {});
}
});

View File

@ -1,7 +1,6 @@
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";
@ -14,8 +13,6 @@ 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()
@ -30,17 +27,13 @@ async function execute(interaction: ChatInputCommandInteraction): Promise<void>
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.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: "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(),
@ -57,7 +50,7 @@ async function execute(interaction: ChatInputCommandInteraction): Promise<void>
interaction.reply({
embeds: [embed],
ephemeral: true
}).catch();
}).catch(() => {});
}
export {

View File

@ -1,8 +1,8 @@
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 getDefaultEmbed, { getFailedEmbed, getSuccessEmbed } from "../tools/defaultEmbeds";
import { database, GuildSetting } from "../data";
import { getGuildSetting } from "../tools/data";
import { getGuildChannel } from "../tools/discord";
import { getGuildChannel, getChannelPermission } from "../tools/discord";
import { Emoji } from "../tools/design";
const builder = new SlashCommandBuilder();
@ -20,7 +20,7 @@ builder.addChannelOption((option) => {
async function resetNotificationChannel(guildSetting: GuildSetting, interaction: ChatInputCommandInteraction): Promise<void> {
const logChannel = guildSetting.notificationChannelID ? await getGuildChannel(guildSetting.id, guildSetting.notificationChannelID) : null;
guildSetting.notificationChannelID = null;
await database.getRepository(GuildSetting).save(guildSetting);
@ -31,11 +31,11 @@ async function resetNotificationChannel(guildSetting: GuildSetting, interaction:
name: "This action was performed by",
value: `${interaction.user.tag} (${interaction.user.id})`
});
if (logChannel && logChannel.isTextBased()) {
logChannel.send({
embeds: [logEmbed]
}).catch();
}).catch(() => {});
}
const embed = getSuccessEmbed();
@ -43,7 +43,7 @@ async function resetNotificationChannel(guildSetting: GuildSetting, interaction:
interaction.reply({
embeds: [embed],
ephemeral: true
}).catch();
}).catch(() => {});
}
const execute = async (interaction: ChatInputCommandInteraction) => {
@ -55,6 +55,20 @@ 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();
@ -68,7 +82,7 @@ const execute = async (interaction: ChatInputCommandInteraction) => {
if (oldLogChannel && oldLogChannel.isTextBased()) {
oldLogChannel.send({
embeds: [embed]
}).catch();
}).catch(() => {});
}
}
@ -84,16 +98,16 @@ const execute = async (interaction: ChatInputCommandInteraction) => {
});
channel.send({
embeds: [ embed ]
}).catch();
embeds: [embed]
}).catch(() => {});
const reply = getSuccessEmbed();
reply.setDescription(`Log channel was set to <#${channel.id}>`);
interaction.reply({
embeds: [ reply ],
embeds: [reply],
ephemeral: true
}).catch();
}).catch(() => {});
return;
}

View File

@ -42,7 +42,7 @@ async function execute(interaction: ChatInputCommandInteraction): Promise<void>
interaction.reply({
embeds: [embed],
ephemeral: true
}).catch();
}).catch(() => {});
if (!settings.notificationChannelID) return;
const logChannel = await getGuildChannel(interaction.guildId, settings.notificationChannelID);
@ -57,7 +57,7 @@ async function execute(interaction: ChatInputCommandInteraction): Promise<void>
logChannel.send({
embeds: [logEmbed]
}).catch();
}).catch(() => {});
}
export {

View File

@ -1,10 +1,10 @@
import { ChatInputCommandInteraction, PermissionFlagsBits, SlashCommandBuilder } from "discord.js";
import { ChatInputCommandInteraction, Guild, PermissionFlagsBits, SlashCommandBuilder } from "discord.js";
import client from "../client";
import { Badword, database } from "../data";
import { getGuildSetting, isPremiumActive } from "../tools/data";
import { getGuildSetting } from "../tools/data";
import getDefaultEmbed from "../tools/defaultEmbeds";
import moment from "moment";
import { Color, Emoji } from "../tools/design";
import { getGuildChannel } from "../tools/discord";
import { getGuildChannel, getChannelPermission } from "../tools/discord";
const builder = new SlashCommandBuilder();
builder.setName("showsettings");
@ -15,8 +15,13 @@ 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 isPremium = await isPremiumActive(settings.isPremiumUntil);
const wordCount = await database.getRepository(Badword).count({
where: {
guildID: interaction.guildId
@ -27,19 +32,15 @@ async function execute(interaction: ChatInputCommandInteraction): Promise<void>
const embed = getDefaultEmbed();
embed.setTitle(`Settings from guild ${interaction.guild?.name || ""} (${interaction.guildId})`);
embed.setDescription(isPremium ? `${Emoji.PREMIUM} your subscription ends in ${moment(settings.isPremiumUntil).fromNow(true)}` : `Consider Premium status to get an increased blocklist`);
embed.setColor(isPremium ? Color.PREMIUM_ORANGE : Color.INFORMING_BLUE);
embed.setDescription("Thanks for using this bot");
embed.setColor(Color.INFORMING_BLUE);
embed.addFields({
name: "Premium",
value: isPremium ? `${Emoji.PREMIUM} active` : `${Emoji.SWITCH_OFF} inactive`,
inline: true
}, {
name: "Logchannel",
value: logChannel && logChannel.isTextBased() ? `<#${logChannel.id}>` : "Not configured",
value: settings.notificationChannelID ? `<#${settings.notificationChannelID}>` : "Not configured",
inline: true
}, {
name: "Words in Blocklist",
value: `${wordCount}/${isPremium ? "100" : "10"}`,
value: `${wordCount}/100`,
inline: true
}, {
name: `Preserve data on server leave is ${settings.preserveDataOnGuildLeave ? "active" : "inactive"}`,
@ -49,10 +50,58 @@ async function execute(interaction: ChatInputCommandInteraction): Promise<void>
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: [embed],
embeds: embeds,
ephemeral: true
}).catch();
}).catch(() => {});
}
export {

View File

@ -1,7 +1,6 @@
import { ChatInputCommandInteraction, PermissionFlagsBits, SlashCommandBuilder } from "discord.js";
import { IsNull } from "typeorm";
import { Badword, database } from "../data";
import { getGuildSetting, isPremiumActive } from "../tools/data";
const builder = new SlashCommandBuilder();
builder.setName("showblocklist");
@ -11,9 +10,6 @@ builder.setDefaultMemberPermissions(PermissionFlagsBits.ManageChannels);
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 guildBadWords = await database.getRepository(Badword).find({
select: {
@ -30,9 +26,9 @@ async function execute(interaction: ChatInputCommandInteraction): Promise<void>
});
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)} ||`,
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();
}).catch(() => {});
}
export {

View 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
`);
}
}

View File

@ -8,9 +8,6 @@ export class GuildSetting {
@Column("varchar", { nullable: true, default: null })
notificationChannelID!: string | null;
@Column("timestamp", { nullable: true, default: null })
isPremiumUntil!: Date | null;
@Column("boolean", { default: false })
preserveDataOnGuildLeave!: boolean
}

109
src/events/channelCreate.ts Normal file
View File

@ -0,0 +1,109 @@
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 } 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 (!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(() => {});
});

View File

@ -1,6 +1,6 @@
import client from "../client";
import { Events } from "discord.js";
import { getGuildSetting, isPremiumActive } from "../tools/data";
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 } from "../tools/defaultEmbeds";
@ -10,11 +10,9 @@ 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: {
@ -29,7 +27,7 @@ client.on(Events.ChannelUpdate, async (oldChannel, newChannel) => {
const blocklist = [...globalBlocklist, ...localBlocklist];
let found: string | null = null;
for (let i = 0; i < blocklist.length; i++) {
const word = blocklist[i];
if (!word) continue;
@ -41,45 +39,71 @@ client.on(Events.ChannelUpdate, async (oldChannel, newChannel) => {
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 {
await newChannel.setName("CENSORED", `[Automated] Detected blocked word in channel name. Name has been censored`);
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 censor <#${newChannel.id}> (${newChannel.id}): ${error instanceof Error ? error.message : error}`);
if (isPremium) embed.addFields({
embed.setDescription(`Couldn't delete <#${newChannel.id}> (${newChannel.id}): ${error instanceof Error ? error.message : error}`);
embed.addFields({
name: "Detected banned word:",
value: `||${found}||`
},{
name: "Old channel name:",
value: `||${name}||`,
inline: true
}, {
name: "Channel renamed by:",
value: responsibleUser ? `${responsibleUser.tag} (${responsibleUser.id})` : "`Couldn't detect responsible user :(`"
});
logChannel.send({
embeds: [embed]
}).catch();
}).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.setDescription(`||#${newChannel.name}|| (${newChannel.id}) has been deleted because its name contained a blocked word.`);
embed.setColor(Color.WARNING_YELLOW);
if (isPremium) embed.addFields({
embed.addFields({
name: "Detected banned word:",
value: `||${found}||`,
inline: true
},{
name: "Old channel name:",
value: `||${name}||`,
inline: true
}, {
name: "Channel renamed by:",
value: responsibleUser ? `${responsibleUser.tag} (${responsibleUser.id})` : "`Couldn't detect responsible user :(`"
});
logChannel.send({
embeds: [embed]
}).catch();
}).catch(() => {});
});

View File

@ -1,2 +1,3 @@
import "./channelUpdate";
import "./guildDelete";
import "./guildDelete";
import "./channelCreate";

View File

@ -10,18 +10,9 @@ export async function getGuildSetting(guildID: string): Promise<GuildSetting> {
if (!guildSetting) {
guildSetting = new GuildSetting();
guildSetting.id = guildID;
guildSetting.isPremiumUntil = null;
guildSetting.notificationChannelID = null;
guildSetting.preserveDataOnGuildLeave = false;
}
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;
}

View File

@ -1,10 +1,25 @@
import { GuildBasedChannel } from "discord.js";
import { GuildBasedChannel, GuildTextBasedChannel, PermissionsBitField } 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;
try {
const guild = await client.guilds.fetch(guildID);
if (!guild) return null;
const channel = await guild.channels.fetch(channelID);
return channel;
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;
}
}