Skip to content

Feature/#95: Prevent channels being misused for bot testing #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/features/commands.ts
Original file line number Diff line number Diff line change
@@ -2,7 +2,11 @@ import fetch from "node-fetch";
import { Message, TextChannel } from "discord.js";
import cooldown from "./cooldown";
import { ChannelHandlers } from "../types";
import { isStaff } from "../utils";
import {
isStaff,
isBotDisabledInChannel,
notifyUserBotResponseWasPrevented
} from "../utils";

const EMBED_COLOR = 7506394;

@@ -622,6 +626,17 @@ const commands: ChannelHandlers = {

if (keyword) {
if (cooldown.hasCooldown(msg.author.id, `commands.${keyword}`)) return;

const { name: channelName } = msg.channel as TextChannel;

const hasPreventedBotResponse = isBotDisabledInChannel(channelName);

if (hasPreventedBotResponse) {
notifyUserBotResponseWasPrevented(msg, channelName);

return;
}

cooldown.addCooldown(msg.author.id, `commands.${keyword}`);
command.handleMessage(msg);
}
25 changes: 24 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GuildMember } from "discord.js";
import { GuildMember, Message } from "discord.js";

const staffRoles = ["mvp", "moderator", "admin", "admins"];

@@ -21,3 +21,26 @@ export const truncateMessage = (

return message;
};

const disabledBotChannelNames = ["random"];

export const isBotDisabledInChannel = (channelName: string) => {
return disabledBotChannelNames.includes(channelName);
};

export const notifyUserBotResponseWasPrevented = async (
message: Message,
channelName: string
) => {
const { author, channel } = message;

try {
await author.send(
`Please do not test bot commands in the #${channelName} channel. You can test them here.`
);
} catch {
await channel.send(
`Please do not test bot commands in the #${channelName} channel. You can direct message me to test them (you may need to enable direct messages from users in this server).`
);
}
};