-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: split manage command in separate commands
- Loading branch information
Showing
4 changed files
with
154 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}.`, | ||
}; | ||
}, | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}.`, | ||
}; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters