Skip to content

Commit

Permalink
Maybe make listids fallback to non-dm channel.
Browse files Browse the repository at this point in the history
  • Loading branch information
itsTheFae committed Feb 7, 2024
1 parent cae63d7 commit 636b867
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions musicbot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4522,10 +4522,8 @@ async def cmd_pldump(

try:
# try to DM. this could fail for users with strict privacy settings.
await author.send(
msg_str,
file=datafile,
)
# or users who just can't get direct messages.
await author.send(msg_str, file=datafile)

except discord.errors.HTTPException as e:
if e.code == 50007: # cannot send to this user.
Expand All @@ -4544,6 +4542,7 @@ async def cmd_listids(
self,
guild: discord.Guild,
author: discord.Member,
channel: MessageableChannel,
leftover_args: List[str],
cat: str = "all",
) -> CommandResponse:
Expand Down Expand Up @@ -4601,15 +4600,28 @@ async def cmd_listids(
if rawudata:
data.extend(rawudata)

sent_to_channel = None
with BytesIO() as sdata:
slug = slugify(guild.name)
fname = f"{slug}-ids-{cat}.txt"
sdata.writelines(d.encode("utf8") + b"\n" for d in data)
sdata.seek(0)
datafile = discord.File(sdata, filename=fname)
msg_str = "Here are the IDs you requested:"

await author.send(file=discord.File(sdata, filename=fname))
try:
# try to DM and fall back to channel
await author.send(msg_str, file=datafile)

return Response("Sent a message with a list of IDs.", delete_after=20)
except discord.errors.HTTPException as e:
if e.code == 50007: # cannot send to this user.
log.debug("DM failed, sending in channel instead.")
sent_to_channel = await channel.send(msg_str, file=datafile)
else:
raise
if not sent_to_channel:
return Response("Sent a message with a list of IDs.", delete_after=20)
return None

async def cmd_perms(
self,
Expand Down

0 comments on commit 636b867

Please sign in to comment.