Skip to content

Commit

Permalink
Merge pull request #501 from uwcsc/Issue466
Browse files Browse the repository at this point in the history
Allow bot to ban users outside server
  • Loading branch information
davidgan1218 authored Dec 6, 2023
2 parents a07018d + 1491363 commit 06db106
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
9 changes: 4 additions & 5 deletions src/commandDetails/admin/ban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '../../codeyCommand';
import { banUser } from '../../components/admin';
import { vars } from '../../config';
import { pluralize } from '../../utils/pluralize';

// Ban a user
const banExecuteCommand: SapphireMessageExecuteType = async (client, messageFromUser, args) => {
Expand All @@ -31,13 +32,11 @@ const banExecuteCommand: SapphireMessageExecuteType = async (client, messageFrom
);
}
const days = <number>args['days'];
// Get the GuildMember object corresponding to the user in the guild
// This is needed because we can only ban GuildMembers, not Users
// get Guild object corresponding to server
const guild = await client.guilds.fetch(vars.TARGET_GUILD_ID);
const memberInGuild = await guild.members.fetch({ user });
if (await banUser(memberInGuild, reason, days)) {
if (await banUser(guild, user, reason, days)) {
return `Successfully banned user ${user.tag} (id: ${user.id}) ${
days ? `and deleted their messages in the past ${days} days ` : ``
days ? `and deleted their messages in the past ${days} ${pluralize('day', days)} ` : ``
}for the following reason: ${reason}`;
} else {
throw new CodeyUserError(
Expand Down
22 changes: 17 additions & 5 deletions src/components/admin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GuildMember } from 'discord.js';
import { Guild, User } from 'discord.js';
import { vars } from '../config';
import { logger } from '../logger/default';
import { pluralize } from '../utils/pluralize';
Expand All @@ -19,16 +19,28 @@ a reason why you think you should be unbanned.
`;

/* Ban a user, returns whether ban was successful */
// Bans user from guild even if they are not in server
// makeBanMessage is only sent to User if they are in server (in Discord, you cannot send direct messages to users who are not in any mutual servers)
export const banUser = async (
member: GuildMember,
guild: Guild,
user: User,
reason: string,
days?: number,
): Promise<boolean> => {
let isSuccessful = false;
try {
const user = member.user;
await user.send(makeBanMessage(reason, days));
await member.ban({ reason, deleteMessageDays: days });
try {
await user.send(makeBanMessage(reason, days));
} catch (err) {
logger.error({
event: "Can't send message to user not in server",
error: (err as Error).toString(),
});
}
await guild.members.ban(user, {
reason: reason,
deleteMessageSeconds: days == null ? 0 : days * 86400,
});
isSuccessful = true;
} catch (err) {
logger.error({
Expand Down

0 comments on commit 06db106

Please sign in to comment.