Skip to content

Commit

Permalink
Add back manager commands (#31)
Browse files Browse the repository at this point in the history
* remove TODO

* move IS_ADMIN to util

* add manager cog
add message content intent requirement to readme
  • Loading branch information
user32121 authored Mar 21, 2024
1 parent f2e4352 commit c9154a1
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Check the 'bot' scope, and after doing so, the list of bot-specific permissions
+ Manage Roles
+ Manage Threads

Additionally, go to the 'Bot' tab and enable the 'Message Content Intent'.

In the future, more permission may be needed, so ask the current maintainer.

Then, copy the link and invite the bot to your server.
Expand Down
3 changes: 1 addition & 2 deletions cogs/ctf/ctf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import interactions
from interactions import Extension, SlashContext

from lib.util import subcommand, sanitize_name, get_ctf_forum
from lib.util import subcommand, sanitize_name, get_ctf_forum, IS_ADMIN
from lib.config import CTF_CATEGORY_CHANNELS, CHALLENGE_CATEGORIES, FORUM_GENERAL_CHANNEL, CTF_ROLES

IS_ADMIN = interactions.slash_default_member_permission(interactions.Permissions.ADMINISTRATOR)


class CTF(Extension):
Expand Down
83 changes: 83 additions & 0 deletions cogs/manager/manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import asyncio
import http.client

import interactions
from interactions import Extension, SlashContext

from lib.util import subcommand, IS_ADMIN


class Manager(Extension):
'''Commands for managing the discord server'''

@subcommand(channel={'description': 'The channel to send the message in', 'channel_types': [interactions.ChannelType.GUILD_TEXT]})
@IS_ADMIN
async def say(self, ctx: SlashContext, channel: interactions.GuildText) -> None:
'''
Says your previous message in the channel you specify
'''
await ctx.defer()
try:
messages = await ctx.channel.history(limit=100).flatten()
message = [m for m in messages if m.author.id == ctx.author.id][0].content
except IndexError:
await ctx.send(":x: Send the message in the current channel before calling /manager edit.")
return

confirm_message = await ctx.send(f'Should I send this in {channel.mention}? (60s expiry):\n{message}\n')

await confirm_message.add_reaction('✅')

def check(event: interactions.events.MessageReactionAdd):
return event.message.id == confirm_message.id and \
event.author == ctx.author and event.emoji.name == '✅'
try:
await self.bot.wait_for(interactions.events.MessageReactionAdd, checks=check, timeout=60.0)
except asyncio.TimeoutError:
await ctx.send(':x: Timed out, did not send.')
return

await channel.send(message)
await ctx.send(f':white_check_mark: sent message in {channel.mention}')

@subcommand(message_channel={'description': 'The channel of the original message', 'channel_types': [interactions.ChannelType.GUILD_TEXT]}, message_id={'description': 'The ID of the message to edit'})
@IS_ADMIN
async def edit(self, ctx: SlashContext, message_channel: interactions.GuildText, message_id: str) -> None:
'''
Edits a message said by the bot, must specify the messageID
'''
await ctx.defer()
try:
bot_message = await message_channel.fetch_message(message_id)
except (http.client.HTTPException, ValueError):
await ctx.send(":x: Invalid message_id")
return
if (bot_message is None):
await ctx.send(":x: Unable to find message")
return
if (bot_message.author != self.bot.user):
await ctx.send(":x: Message must be from the bot")
return

try:
messages = await ctx.channel.history(limit=100).flatten()
message = [m for m in messages if m.author.id == ctx.author.id][0].content
except IndexError:
await ctx.send(":x: Send the message in the current channel before calling /manager edit.")
return

confirm_message = await ctx.send(f'Should I edit the message you specified to say this? (60s expiry):\n{message}\n')

await confirm_message.add_reaction('✅')

def check(event: interactions.events.MessageReactionAdd):
return event.message.id == confirm_message.id and \
event.author == ctx.author and event.emoji.name == '✅'
try:
await self.bot.wait_for(interactions.events.MessageReactionAdd, checks=check, timeout=60.0)
except asyncio.TimeoutError:
await ctx.send(':x: Timed out, did not edit message.')
return

await bot_message.edit(content=message)
await ctx.send(f':white_check_mark: edited message.')
2 changes: 2 additions & 0 deletions lib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

from lib.config import GUILD_IDS

IS_ADMIN = interactions.slash_default_member_permission(interactions.Permissions.ADMINISTRATOR)

option_types = set(item.value for item in OptionType)


Expand Down
1 change: 0 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ async def on_slash_command_error(event: interactions.api.events.CommandError) ->
ctx = event.ctx
if isinstance(err, interactions.client.errors.NotFound):
pass
# TODO figure out error types
elif isinstance(err, interactions.client.errors.Forbidden):
await ctx.send(":x: I don't have enough privileges to perform this action.")
elif isinstance(err, interactions.client.errors.InteractionMissingAccess):
Expand Down

0 comments on commit c9154a1

Please sign in to comment.