Rework bot function #13

Merged
AstroGD merged 15 commits from feature/12-show-warning-if-logchannel-is-unreachable into dev 2022-11-29 03:07:01 +01:00
8 changed files with 12 additions and 69 deletions
Showing only changes of commit ee9039c239 - Show all commits

View File

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

View File

@ -1,7 +1,7 @@
import { ChatInputCommandInteraction, PermissionFlagsBits, SlashCommandBuilder } from "discord.js"; import { ChatInputCommandInteraction, PermissionFlagsBits, SlashCommandBuilder } from "discord.js";
import { database, Badword } from "../data"; import { database, Badword } from "../data";
import { IsNull } from "typeorm"; import { IsNull } from "typeorm";
import { getGuildSetting, isPremiumActive } from "../tools/data"; import { getGuildSetting } from "../tools/data";
import getDefaultEmbed, { getFailedEmbed, getSuccessEmbed } from "../tools/defaultEmbeds"; import getDefaultEmbed, { getFailedEmbed, getSuccessEmbed } from "../tools/defaultEmbeds";
import { getGuildChannel } from "../tools/discord"; import { getGuildChannel } from "../tools/discord";
import { Color, Emoji } from "../tools/design"; 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"); if (!interaction.guildId) throw new Error("Command was executed in DM context");
const settings = await getGuildSetting(interaction.guildId); const settings = await getGuildSetting(interaction.guildId);
const isPremium = isPremiumActive(settings.isPremiumUntil);
const logChannel = settings.notificationChannelID ? await getGuildChannel(interaction.guildId, settings.notificationChannelID) : null; const logChannel = settings.notificationChannelID ? await getGuildChannel(interaction.guildId, settings.notificationChannelID) : null;
@ -63,7 +62,7 @@ async function execute(interaction: ChatInputCommandInteraction): Promise<void>
} }
}); });
const limit = isPremium ? 100 : 10; const limit = 100;
if (count >= limit) { if (count >= limit) {
const embed = getFailedEmbed(); const embed = getFailedEmbed();
embed.setDescription(`You reached the word limit for your guild. Please delete a word before adding a new one`); embed.setDescription(`You reached the word limit for your guild. Please delete a word before adding a new one`);

View File

@ -1,7 +1,6 @@
import { ChatInputCommandInteraction, SlashCommandBuilder } from "discord.js"; import { ChatInputCommandInteraction, SlashCommandBuilder } from "discord.js";
import { IsNull } from "typeorm"; import { IsNull } from "typeorm";
import { Badword, database } from "../data"; import { Badword, database } from "../data";
import { getGuildSetting, isPremiumActive } from "../tools/data";
import getDefaultEmbed from "../tools/defaultEmbeds"; import getDefaultEmbed from "../tools/defaultEmbeds";
import pack from "../../package.json"; import pack from "../../package.json";
import { Color, Emoji } from "../tools/design"; import { Color, Emoji } from "../tools/design";
@ -14,8 +13,6 @@ builder.setDMPermission(false);
async function execute(interaction: ChatInputCommandInteraction): Promise<void> { async function execute(interaction: ChatInputCommandInteraction): Promise<void> {
if (!interaction.inGuild()) throw new Error("Command was executed outside guild context"); 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({ const globalBlockedWordsCount = await database.getRepository(Badword).count({
where: { where: {
guildID: IsNull() guildID: IsNull()
@ -30,17 +27,13 @@ async function execute(interaction: ChatInputCommandInteraction): Promise<void>
const embed = getDefaultEmbed(); const embed = getDefaultEmbed();
embed.setTitle(`${Emoji.SECURITY_CHALLENGE} Channel filter V${pack.version} by AstroGD®`); embed.setTitle(`${Emoji.SECURITY_CHALLENGE} Channel filter V${pack.version} by AstroGD®`);
embed.setDescription(`Codename eu.astrogd.white-leopard`); embed.setDescription(`Codename eu.astrogd.white-leopard`);
embed.setColor(isPremium ? Color.PREMIUM_ORANGE : Color.INFORMING_BLUE); embed.setColor(Color.INFORMING_BLUE);
embed.addFields({ embed.addFields({
name: "What does this bot do?", name: "What does this bot do?",
value: "This bot checks for blocked words contained in channel names and reverts the changes if found." value: "This bot checks for blocked words contained in channel names and reverts the changes if found."
},{ },{
name: "Author", name: "Author",
value: `This bot was created by AstroGD#0001 ${Emoji.ASTROGD} mainly for the official r/Overwatch2 Subreddit Discord server` 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", name: "Global word count",
value: globalBlockedWordsCount.toString(), value: globalBlockedWordsCount.toString(),

View File

@ -1,8 +1,7 @@
import { ChatInputCommandInteraction, PermissionFlagsBits, SlashCommandBuilder } from "discord.js"; import { ChatInputCommandInteraction, PermissionFlagsBits, SlashCommandBuilder } from "discord.js";
import { Badword, database } from "../data"; import { Badword, database } from "../data";
import { getGuildSetting, isPremiumActive } from "../tools/data"; import { getGuildSetting } from "../tools/data";
import getDefaultEmbed from "../tools/defaultEmbeds"; import getDefaultEmbed from "../tools/defaultEmbeds";
import moment from "moment";
import { Color, Emoji } from "../tools/design"; import { Color, Emoji } from "../tools/design";
import { getGuildChannel, getChannelPermission } from "../tools/discord"; import { getGuildChannel, getChannelPermission } from "../tools/discord";
@ -16,7 +15,6 @@ async function execute(interaction: ChatInputCommandInteraction): Promise<void>
if (!interaction.inGuild()) throw new Error("Interaction was performed outside guild context"); if (!interaction.inGuild()) throw new Error("Interaction was performed outside guild context");
const settings = await getGuildSetting(interaction.guildId); const settings = await getGuildSetting(interaction.guildId);
const isPremium = await isPremiumActive(settings.isPremiumUntil);
const wordCount = await database.getRepository(Badword).count({ const wordCount = await database.getRepository(Badword).count({
where: { where: {
guildID: interaction.guildId guildID: interaction.guildId
@ -27,19 +25,15 @@ async function execute(interaction: ChatInputCommandInteraction): Promise<void>
const embed = getDefaultEmbed(); const embed = getDefaultEmbed();
embed.setTitle(`Settings from guild ${interaction.guild?.name || ""} (${interaction.guildId})`); 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.setDescription("Thanks for using this bot");
embed.setColor(isPremium ? Color.PREMIUM_ORANGE : Color.INFORMING_BLUE); embed.setColor(Color.INFORMING_BLUE);
embed.addFields({ embed.addFields({
name: "Premium",
value: isPremium ? `${Emoji.PREMIUM} active` : `${Emoji.SWITCH_OFF} inactive`,
inline: true
}, {
name: "Logchannel", name: "Logchannel",
value: settings.notificationChannelID ? `<#${settings.notificationChannelID}>` : "Not configured", value: settings.notificationChannelID ? `<#${settings.notificationChannelID}>` : "Not configured",
inline: true inline: true
}, { }, {
name: "Words in Blocklist", name: "Words in Blocklist",
value: `${wordCount}/${isPremium ? "100" : "10"}`, value: `${wordCount}/100`,
inline: true inline: true
}, { }, {
name: `Preserve data on server leave is ${settings.preserveDataOnGuildLeave ? "active" : "inactive"}`, name: `Preserve data on server leave is ${settings.preserveDataOnGuildLeave ? "active" : "inactive"}`,

View File

@ -1,7 +1,7 @@
import { ChatInputCommandInteraction, PermissionFlagsBits, SlashCommandBuilder } from "discord.js"; import { ChatInputCommandInteraction, PermissionFlagsBits, SlashCommandBuilder } from "discord.js";
import { IsNull } from "typeorm"; import { IsNull } from "typeorm";
import { Badword, database } from "../data"; import { Badword, database } from "../data";
import { getGuildSetting, isPremiumActive } from "../tools/data"; import { getGuildSetting } from "../tools/data";
const builder = new SlashCommandBuilder(); const builder = new SlashCommandBuilder();
builder.setName("showblocklist"); builder.setName("showblocklist");
@ -12,9 +12,6 @@ builder.setDefaultMemberPermissions(PermissionFlagsBits.ManageChannels);
async function execute(interaction: ChatInputCommandInteraction): Promise<void> { async function execute(interaction: ChatInputCommandInteraction): Promise<void> {
if (!interaction.inGuild()) throw new Error("Command was executed outside guild context"); 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({ const guildBadWords = await database.getRepository(Badword).find({
select: { select: {
value: true value: true
@ -30,7 +27,7 @@ async function execute(interaction: ChatInputCommandInteraction): Promise<void>
}); });
interaction.reply({ 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 ephemeral: true
}).catch(() => {}); }).catch(() => {});
} }

View File

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

View File

@ -1,6 +1,6 @@
import client from "../client"; import client from "../client";
import { Events } from "discord.js"; import { Events } from "discord.js";
import { getGuildSetting, isPremiumActive } from "../tools/data"; import { getGuildSetting } from "../tools/data";
import { Badword, database } from "../data"; import { Badword, database } from "../data";
import { IsNull } from "typeorm"; import { IsNull } from "typeorm";
import getDefaultEmbed, { getFailedEmbed } from "../tools/defaultEmbeds"; import getDefaultEmbed, { getFailedEmbed } from "../tools/defaultEmbeds";
@ -14,7 +14,6 @@ client.on(Events.ChannelUpdate, async (oldChannel, newChannel) => {
const guild = oldChannel.guild; const guild = oldChannel.guild;
const settings = await getGuildSetting(guild.id); const settings = await getGuildSetting(guild.id);
const isPremium = isPremiumActive(settings.isPremiumUntil);
const globalBlocklist = await database.getRepository(Badword).find({ const globalBlocklist = await database.getRepository(Badword).find({
where: { where: {
@ -69,7 +68,7 @@ client.on(Events.ChannelUpdate, async (oldChannel, newChannel) => {
embed.setTitle(`${Emoji.SECURITY_CHALLENGE_FAILED} Blocked word detected`); 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.id}> (${newChannel.id}) has been renamed because its name contained a blocked word.`);
embed.setColor(Color.WARNING_YELLOW); embed.setColor(Color.WARNING_YELLOW);
if (isPremium) embed.addFields({ embed.addFields({
name: "Detected banned word:", name: "Detected banned word:",
value: `||${found}||`, value: `||${found}||`,
inline: true inline: true

View File

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