Skip to content

Commit

Permalink
[ButtonPoll] Improve permission checks
Browse files Browse the repository at this point in the history
Closes #126
  • Loading branch information
Vexed01 committed Jan 6, 2024
1 parent 67d87d6 commit cdd7dc5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
10 changes: 9 additions & 1 deletion buttonpoll/buttonpoll.py
Expand Up @@ -126,7 +126,6 @@ async def buttonpoll(self, ctx: commands.Context, chan: Optional[TextChannel] =
assert isinstance(channel, (TextChannel, discord.Thread))
assert isinstance(ctx.author, discord.Member) # we are in a guild...

# these two checks are untested :)
if not channel.permissions_for(ctx.author).send_messages: # type:ignore
return await ctx.send(
f"You don't have permission to send messages in {channel.mention}, so I can't "
Expand All @@ -137,6 +136,11 @@ async def buttonpoll(self, ctx: commands.Context, chan: Optional[TextChannel] =
f"I don't have permission to send messages in {channel.mention}, so I can't "
"start a poll there."
)
if not channel.permissions_for(ctx.me).attach_files: # type:ignore
await ctx.send(
"\N{WARNING SIGN}\N{VARIATION SELECTOR-16} I don't have permission to attach "
"files in that channel. I won't be able to send a pie chart."
)

view = StartSetupView(author=ctx.author, channel=channel, cog=self)
await ctx.send("Click below to start a poll!", view=view)
Expand All @@ -158,6 +162,10 @@ async def advstartpoll(self, ctx: commands.Context, *, arguments: str = ""):
\N{WARNING SIGN}\N{VARIATION SELECTOR-16} **This command is for advanced users only.
You should use `[p]buttonpoll` or the slash command `poll` for a more user-friendly experience.**
\N{WARNING SIGN}\N{VARIATION SELECTOR-16} This command does not check for permissions. Please
check I have permission to send messages in the channel you want to start the poll in. I'll also
need permission to attach files if you want a pie chart.
**Required arguments:**
- `--channel ID`: The channel ID to start the poll in
- `--question string`: The question to ask
Expand Down
13 changes: 10 additions & 3 deletions buttonpoll/poll.py
Expand Up @@ -228,10 +228,17 @@ async def finish(self):
)
)

plot = await self.plot()
if channel.permissions_for(guild.me).attach_files:
plot = await self.plot()
embed.set_image(url="attachment://plot.png")
message = await channel.send(embed=embed, file=plot, view=view)
else:
message = await channel.send(embed=embed, view=view)
log.warning(
f"Channel {self.channel_id} does not allow me to attach files. "
f"I was unable to attach a pie chart to poll {self.unique_poll_id}."
)

embed.set_image(url="attachment://plot.png")
message = await channel.send(embed=embed, file=plot, view=view)
view.stop()

view2 = discord.ui.View()
Expand Down

0 comments on commit cdd7dc5

Please sign in to comment.