Skip to content

Commit

Permalink
feat: split manage command in separate commands
Browse files Browse the repository at this point in the history
  • Loading branch information
mastondzn committed Jun 23, 2024
1 parent a1a9c49 commit 80793f8
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 119 deletions.
78 changes: 78 additions & 0 deletions bot/src/commands/ban.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { createCommand } from '~/helpers/command/define';
import { schemas } from '~/helpers/schemas';
import { permissions } from '~/providers/permissions';
import { helix } from '~/services/apis/helix';

export default createCommand({
name: 'ban',
description: 'Bans a user from using the bot.',
permissions: [
{ local: 'ambassador' }, //
{ global: 'owner' },
],
usage: [
['ban <user>', 'Bans the user from using the bot, in the current channel.'],
['ban <user> in:<channel>', 'Bans the user from using the bot, in the specified channel.'],
],
options: {
channel: {
schema: schemas.twitch.login().optional(),
aliases: ['in', 'c'],
},
},
arguments: [schemas.twitch.login()],
run: async ({ user, channel, options, args: [target] }) => {
const [wantedChannel, targetUser] = await Promise.all([
helix.users.getUserByName(options.channel ?? channel.login),
helix.users.getUserByName(target),
]);

if (!wantedChannel) {
return { reply: 'Channel not found.' };
}

if (!targetUser) {
return { reply: 'User not found.' };
}

const targetIsSelf = user.login === target;
if (targetIsSelf) {
return { reply: "You can't ban yourself." };
}

const targetIsBroadcaster = target === wantedChannel.name;
if (targetIsBroadcaster) {
return { reply: "You can't ban the broadcaster." };
}

const targetLocalPermission =
(await permissions.getDbLocalPermission(wantedChannel.id, targetUser.id)) ?? 'normal';
if (targetLocalPermission === 'ambassador') {
return {
reply: `You can't ban ${targetUser.displayName}, as they are an ambassador.`,
};
}

const targetIsBotOwner = (await permissions.getGlobalPermission(targetUser.id)) === 'owner';
if (targetIsBotOwner) {
return {
reply: `You can't ban ${targetUser.displayName}, as they are a bot owner.`,
};
}

if (targetLocalPermission === 'banned') {
return {
reply: `User ${targetUser.displayName} is already banned from using the bot locally.`,
};
}

await permissions.setLocalPermission('banned', {
channel: { id: wantedChannel.id, login: wantedChannel.name },
user: { id: targetUser.id, login: targetUser.name },
});

return {
reply: `Banned ${targetUser.displayName} from using the bot in ${wantedChannel.name}.`,
};
},
});
117 changes: 0 additions & 117 deletions bot/src/commands/manage.ts

This file was deleted.

74 changes: 74 additions & 0 deletions bot/src/commands/unban.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { createCommand } from '~/helpers/command/define';
import { schemas } from '~/helpers/schemas';
import { permissions } from '~/providers/permissions';
import { helix } from '~/services/apis/helix';

export default createCommand({
name: 'unban',
description: 'Unbans a user from using the bot.',
permissions: [
{ local: 'ambassador' }, //
{ global: 'owner' },
],
usage: [
['unban <user>', 'Unbans the user from using the bot, in the current channel.'],
[
'unban <user> in:<channel>',
'Unbans the user from using the bot, in the specified channel.',
],
],
options: {
channel: {
schema: schemas.twitch.login().optional(),
aliases: ['in', 'c'],
},
},
arguments: [schemas.twitch.login()],
run: async ({ user, channel, options, args: [target] }) => {
const [wantedChannel, targetUser] = await Promise.all([
helix.users.getUserByName(options.channel ?? channel.login),
helix.users.getUserByName(target),
]);

if (!wantedChannel) {
return { reply: 'Channel not found.' };
}

if (!targetUser) {
return { reply: 'User not found.' };
}

const targetIsSelf = user.login === target;
if (targetIsSelf) {
return { reply: "You can't unban yourself." };
}

const targetIsBroadcaster = target === wantedChannel.name;
if (targetIsBroadcaster) {
return { reply: "You can't unban the broadcaster." };
}

const targetIsBotOwner = (await permissions.getGlobalPermission(targetUser.id)) === 'owner';
if (targetIsBotOwner) {
return { reply: `You can't ban ${targetUser.displayName}, as they are a bot owner.` };
}

const currentTargetPermission =
(await permissions.getDbLocalPermission(wantedChannel.id, targetUser.id)) ?? 'normal';

if (currentTargetPermission !== 'banned') {
return {
reply: `You can't unban ${targetUser.displayName} as they are not banned from using the bot locally.`,
};
}

await permissions.setLocalPermission('normal', {
channel: { id: wantedChannel.id, login: wantedChannel.name },
user: { id: targetUser.id, login: targetUser.name },
});

return {
reply: `Unbanned ${targetUser.displayName} from using the bot in ${wantedChannel.name}.`,
};
},
});
4 changes: 2 additions & 2 deletions bot/src/helpers/command/parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { splitOnce } from '../string';
import { trim } from '../tags';
import { UserError } from '~/errors/user';

export function getWantedCommand({ messageText: text }: Pick<PrivmsgMessage, 'messageText'>) {
return splitOnce(text.replace(prefix, ''), ' ')[0];
export function getWantedCommand({ messageText }: Pick<PrivmsgMessage, 'messageText'>) {
return splitOnce(messageText.replace(prefix, ''), ' ')[0];
}

export interface CommandParameters {
Expand Down

0 comments on commit 80793f8

Please sign in to comment.