Skip to content
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

Allow bot to ban users outside server #501

Merged
merged 4 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 3 additions & 5 deletions src/commandDetails/admin/ban.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CodeyUserError } from './../../codeyUserError';
import { container } from '@sapphire/framework';
import { PermissionsBitField, User } from 'discord.js';
import { PermissionsBitField, User, GuildMemberManager } from 'discord.js';
import {
CodeyCommandDetails,
CodeyCommandOptionType,
Expand Down Expand Up @@ -31,11 +31,9 @@ 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 ` : ``
Fan-Yang-284 marked this conversation as resolved.
Show resolved Hide resolved
}for the following reason: ${reason}`;
Expand Down
21 changes: 15 additions & 6 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 @@ -9,7 +9,7 @@ const MOD_USER_ID_FOR_BAN_APPEAL: string = vars.MOD_USER_ID_FOR_BAN_APPEAL;
const makeBanMessage = (reason: string, days?: number): string =>
`
Uh oh, you have been banned from the UW Computer Science Club server ${
days && `and your messages in the past ${days} ${pluralize('day', days)} have been deleted`
days && `and your messages in the past ${days} ${pluralize('day', days)} have been deleted `
}for the following reason:

> ${reason}
Expand All @@ -19,16 +19,25 @@ 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