From 1cb7525bb9304a13912fd743fc6e6551ddd31125 Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Mon, 7 Aug 2023 21:29:22 -0400 Subject: [PATCH] feat: added say cog --- say/__init__.py | 5 ++++ say/info.json | 9 +++++++ say/say.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 say/__init__.py create mode 100644 say/info.json create mode 100644 say/say.py diff --git a/say/__init__.py b/say/__init__.py new file mode 100644 index 0000000..1624e51 --- /dev/null +++ b/say/__init__.py @@ -0,0 +1,5 @@ +from .say import Say + + +async def setup(bot): + await bot.add_cog(Say(bot)) diff --git a/say/info.json b/say/info.json new file mode 100644 index 0000000..e3e9e47 --- /dev/null +++ b/say/info.json @@ -0,0 +1,9 @@ +{ + "author" : ["SeaswimmerTheFsh"], + "install_msg" : "Thank you for installing Info!\nYou can find the source code of this cog here: https://github.com/SeaswimmerTheFsh/GalaxyCogs", + "name" : "Info", + "short" : "Provides information on Discord objects.", + "description" : "Provides information on Discord objects. Most of this code is shamelessly ripped from .", + "end_user_data_statement" : "This cog does not store any End User Data." + } + \ No newline at end of file diff --git a/say/say.py b/say/say.py new file mode 100644 index 0000000..d6051a5 --- /dev/null +++ b/say/say.py @@ -0,0 +1,67 @@ +from typing import Union +import discord +from redbot.core import commands, checks, app_commands + +class Say(commands.Cog): + """Allows you to send messages as the bot account.""" + + def __init__(self, bot): + self.bot = bot + + async def send_to_target(self, target: Union[discord.Member, discord.TextChannel], interaction: discord.Interaction, message: str, secondary_message: str = None): + if isinstance(target, discord.Member): + target_type = "member" + elif isinstance(target, discord.TextChannel): + target_type = "textchannel" + try: + await target.send(message) + if secondary_message.value != "": + await target.send(secondary_message) + await interaction.response.send_message(content=f"Message sent to {target.mention}!\nMessage contents:\n```{message}```\n```{secondary_message}```", ephemeral=True) + else: + await interaction.response.send_message( + content=f"Message sent to {target.mention}!\nMessage contents:\n```{message}```", ephemeral=True) + except (discord.HTTPException, discord.Forbidden) as error: + if target_type == "member": + await interaction.response.send_message(content="That user has their direct messages closed!", ephemeral=True) + elif target_type == "textchannel": + await interaction.response.send_message(content="I cannot access that channel!", ephemeral=True) + + class MessageModal(discord.ui.Modal, title="Sending message..."): + def __init__(self, target): + super().__init__() + self.target = target + message = discord.ui.TextInput( + label="Message Content", + placeholder="I'm contacting you about your cars extended warranty...", + style=discord.TextStyle.paragraph, + max_length=1750 + ) + secondary_message = discord.ui.TextInput( + label="Secondary Message Content", + placeholder="Typically used for images/image links.", + style=discord.TextStyle.short, + required=False, + max_length=200 + ) + + async def on_submit(self, interaction: discord.Interaction): + Say.send_to_target(self, self.target, interaction, self.message, self.secondary_message) + + send = app_commands.Group(name="send", description="Send a message as the bot user!") + + @send.command(name="user", description="Sends a direct message to a user.") + async def user(self, interaction: discord.Interaction, member: discord.Member, message: str = None): + """Sends a direct message to a user.""" + if message: + await Say.send_to_target(self, member, interaction, message) + else: + await interaction.response.send_modal(Say.MessageModal(member)) + + @send.command(name="channel", description="Sends a message to a channel.") + async def channel(self, interaction: discord.Interaction, channel: discord.TextChannel, message: str = None): + """Sends a message to a channel.""" + if message: + await Say.send_to_target(self, channel, interaction, message) + else: + await interaction.response.send_modal(Say.MessageModal(channel))