GalaxyCogs/send/send.py

67 lines
3.2 KiB
Python
Raw Permalink Normal View History

2023-08-07 21:29:22 -04:00
from typing import Union
import discord
2023-09-24 17:17:30 -04:00
from redbot.core import commands, app_commands
2023-08-07 21:38:35 -04:00
class Send(commands.Cog):
2023-08-07 21:29:22 -04:00
"""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 is not None:
2023-08-07 21:29:22 -04:00
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:
2023-08-07 21:32:44 -04:00
await interaction.response.send_message(content=f"Message sent to {target.mention}!\nMessage contents:\n```{message}```", ephemeral=True)
2023-09-24 17:17:30 -04:00
except (discord.HTTPException, discord.Forbidden):
2023-08-07 21:36:32 -04:00
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)
2023-08-07 21:29:22 -04:00
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):
2023-08-07 21:38:35 -04:00
await Send.send_to_target(self, self.target, interaction, self.message, self.secondary_message)
2023-08-07 21:29:22 -04:00
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:
2023-08-07 21:38:35 -04:00
await Send.send_to_target(self, member, interaction, message)
2023-08-07 21:29:22 -04:00
else:
2023-08-07 21:38:35 -04:00
await interaction.response.send_modal(Send.MessageModal(member))
2023-08-07 21:29:22 -04:00
@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:
2023-08-07 21:38:35 -04:00
await Send.send_to_target(self, channel, interaction, message)
2023-08-07 21:29:22 -04:00
else:
2023-08-07 21:38:35 -04:00
await interaction.response.send_modal(Send.MessageModal(channel))