Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async applyBotSlowmodeToUserId(channel: GuildChannel & TextChannel, userId: string) {
// Deny sendMessage permission from the user. If there are existing permission overwrites, take those into account.
const existingOverride = channel.permissionOverwrites.get(userId);
const newDeniedPermissions =
(existingOverride ? existingOverride.deny : 0) | ErisConstants.Permissions.sendMessages;
const newAllowedPermissions =
(existingOverride ? existingOverride.allow : 0) & ~ErisConstants.Permissions.sendMessages;
try {
await channel.editPermission(userId, newAllowedPermissions, newDeniedPermissions, "member");
} catch (e) {
const user = this.bot.users.get(userId) || new UnknownUser({ id: userId });
if (e instanceof DiscordRESTError && e.code === 50013) {
logger.warn(
`Missing permissions to apply bot slowmode to user ${userId} on channel ${channel.name} (${channel.id}) on server ${this.guild.name} (${this.guildId})`,
);
this.logs.log(LogType.BOT_ALERT, {
body: `Missing permissions to apply bot slowmode to {userMention(user)} in {channelMention(channel)}`,
user: stripObjectToScalars(user),
channel: stripObjectToScalars(channel),
async applyBotSlowmodeToUserId(channel: GuildChannel & TextChannel, userId: string) {
// Deny sendMessage permission from the user. If there are existing permission overwrites, take those into account.
const existingOverride = channel.permissionOverwrites.get(userId);
const newDeniedPermissions =
(existingOverride ? existingOverride.deny : 0) | ErisConstants.Permissions.sendMessages;
const newAllowedPermissions =
(existingOverride ? existingOverride.allow : 0) & ~ErisConstants.Permissions.sendMessages;
try {
await channel.editPermission(userId, newAllowedPermissions, newDeniedPermissions, "member");
} catch (e) {
const user = this.bot.users.get(userId) || new UnknownUser({ id: userId });
if (e instanceof DiscordRESTError && e.code === 50013) {
logger.warn(
`Missing permissions to apply bot slowmode to user ${userId} on channel ${channel.name} (${channel.id}) on server ${this.guild.name} (${this.guildId})`,
);
this.logs.log(LogType.BOT_ALERT, {
body: `Missing permissions to apply bot slowmode to {userMention(user)} in {channelMention(channel)}`,
user: stripObjectToScalars(user),
channel: stripObjectToScalars(channel),
});
} else {
hasPermission(permission: string, target: PermissionTargets = PermissionTargets.Self): boolean {
if (!Object.keys(Constants.Permissions).includes(permission)) {
if (this.client.extensions.logger) this.client.extensions.logger.dispatch('warn', `Unknown permission "${permission}"`);
return true;
}
switch (target) {
case PermissionTargets.Self:
return this.channel.permissionsOf(this._client.user.id).has(permission);
case PermissionTargets.Author:
return this.channel.permissionsOf(this.author.id).has(permission);
case PermissionTargets.Both:
return this.hasPermission(permission) && this.hasPermission(permission, PermissionTargets.Author);
default:
throw new Error(`Unknown target: ${target}`);
}
}
}
static gather(permission) {
if (!permission)
return this.gather(0);
if (typeof permission === 'number' && permission >= 0)
return permission;
if (permission instanceof Array)
return permission.map(p => this.gather(p)).reduce((previous, p) => previous | p, 0);
if (typeof permission === 'string')
return Constants.Permissions[permission];
throw new RangeError('Invalid permission bitfield.');
}