Skip to content

Update echo to be a slash command #1288

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
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
85 changes: 44 additions & 41 deletions techsupport_bot/commands/echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@
MessageEcho

This file contains 2 commands:
.echo user
.echo channel
/echo user
/echo channel
"""

from __future__ import annotations

import discord.abc
from discord import app_commands
from discord.ext import commands

from typing import TYPE_CHECKING, Self

from core import auxiliary, cogs
from discord.ext import commands

if TYPE_CHECKING:
import bot
Expand All @@ -25,82 +28,82 @@
Args:
bot (bot.TechSupportBot): The bot object to register the cogs to
"""
await bot.add_cog(MessageEcho(bot=bot))
await bot.add_cog(MessageEcho(bot=bot, extension_name="echo"))


class MessageEcho(cogs.BaseCog):
"""
The class that holds the echo commands
"""

@commands.check(auxiliary.bot_admin_check_context)
@commands.group(
brief="Executes an echo bot command", description="Executes an echo bot command"
echo: app_commands.Group = app_commands.Group(
name="echo", description="...", extras={"module": "echo"}
)
async def echo(self: Self, ctx: commands.Context) -> None:
"""The bare .echo command. This does nothing but generate the help message

Args:
ctx (commands.Context): The context in which the command was run in
"""

# Executed if there are no/invalid args supplied
await auxiliary.extension_help(self, ctx, self.__module__[9:])

@auxiliary.with_typing
@commands.check(auxiliary.bot_admin_check_context)
@echo.command(
name="channel",
description="Echos a message to a channel",
usage="[channel-id] [message]",
extras={"module": "echo"},
)
async def echo_channel(
self: Self, ctx: commands.Context, channel_id: int, *, message: str
self: Self,
interaction: discord.Interaction,
channel: discord.Thread | discord.TextChannel | discord.VoiceChannel,
*,
message: str,
) -> None:
"""Sends a message to a specified channel.

This is a command and should be accessed via Discord.

Args:
ctx (commands.Context): the context object for the calling message
channel_id (int): the ID of the channel to send the echoed message
interaction (discord.Interaction): the associated interaction
channel (discord.Thread|discord.TextChannel|discord.VoiceChannel): channel to send the message to

Check notice on line 62 in techsupport_bot/commands/echo.py

View check run for this annotation

codefactor.io / CodeFactor

techsupport_bot/commands/echo.py#L62

Line too long (109/100) (line-too-long)
message (str): the message to echo
"""
channel = self.bot.get_channel(channel_id)
if not channel:
await auxiliary.send_deny_embed(
message="I couldn't find that channel", channel=ctx.channel
)
try:
await channel.send(content=message)
except discord.Forbidden:
embed = auxiliary.prepare_deny_embed(message="Unable to send message")
await interaction.response.send_message(embed=embed)
return

await channel.send(content=message)
embed = auxiliary.prepare_confirm_embed(message="Message sent!")
await interaction.response.send_message(embed=embed, ephemeral=True)

await auxiliary.send_confirm_embed(message="Message sent", channel=ctx.channel)

@auxiliary.with_typing
@commands.check(auxiliary.bot_admin_check_context)
@echo.command(
name="user",
description="Echos a message to a user",
usage="[user-id] [message]",
extras={"module": "echo"},
)
async def echo_user(
self: Self, ctx: commands.Context, user_id: int, *, message: str
self: Self, interaction: discord.Interaction, user: discord.User, message: str
) -> None:
"""Sends a message to a specified user.

This is a command and should be accessed via Discord.

Args:
ctx (commands.Context): the context object for the calling message
user_id (int): the ID of the user to send the echoed message
interaction (discord.Interaction): the associated interaction
user (discord.User): the the user to send the echoed message
message (str): the message to echo
"""
user = await self.bot.fetch_user(int(user_id))
if not user:
await auxiliary.send_deny_embed(
message="I couldn't find that user", channel=ctx.channel
)

if user.bot:
embed = auxiliary.prepare_deny_embed(message="You cannot message a bot")
await interaction.response.send_message(embed=embed)
return

await user.send(content=message)
try:
await user.send(content=message)
except discord.Forbidden:
embed = auxiliary.prepare_deny_embed(
message="Unable to send message to this user"
)
await interaction.response.send_message(embed=embed)
return

await auxiliary.send_confirm_embed(message="Message sent", channel=ctx.channel)
embed = auxiliary.prepare_confirm_embed(message="Message sent!")
await interaction.response.send_message(embed=embed, ephemeral=True)
Loading