Skip to content

Commit

Permalink
fix(gaw): check bot permissions before sending
Browse files Browse the repository at this point in the history
  • Loading branch information
ZRunner committed Oct 11, 2023
1 parent 4ce3e2a commit bc213d3
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/modules/giveaways/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import random
from datetime import timedelta
from typing import Optional
from typing import Optional, Union
from uuid import uuid4

import discord
Expand All @@ -15,6 +15,9 @@
from src.modules.giveaways.types import GiveawayData, GiveawayToSendData
from src.modules.giveaways.views import GiveawayView

AcceptableChannel = (discord.TextChannel, discord.Thread, discord.StageChannel, discord.VoiceChannel)
AcceptableChannelType = Union[discord.TextChannel, discord.Thread, discord.StageChannel, discord.VoiceChannel]


class GiveawaysCog(commands.Cog):
"Handle giveaways"
Expand Down Expand Up @@ -124,18 +127,22 @@ async def gw_list(self, interaction: COInteraction, *, include_stopped: bool=Fal

@group.command(name="create")
async def gw_create(self, interaction: COInteraction, *, name: str, description: str,
duration: DurationOption, channel: Optional[discord.TextChannel]=None,
duration: DurationOption, channel: Optional[AcceptableChannelType]=None,
color: Optional[ColorOption]=None, max_entries: Optional[int]=None,
winners_count: int=1):
"Create a giveaway"
if interaction.guild is None:
return
target_channel = channel or interaction.channel
if not isinstance(target_channel, discord.TextChannel):
if not isinstance(target_channel, AcceptableChannel):
await interaction.response.send_message("Giveaways can only be sent in text channels!")
return
if target_channel is None:
return
bot_perms = target_channel.permissions_for(interaction.guild.me)
if not (bot_perms.send_messages and bot_perms.embed_links):
await interaction.response.send_message("I need the permission to send messages and embed links in this channel!")
return
await interaction.response.defer()
ends_date = discord.utils.utcnow() + timedelta(seconds=duration)
if max_entries is not None and winners_count > max_entries:
Expand Down Expand Up @@ -219,7 +226,7 @@ async def create_active_gaw_embed(self, data: GiveawayToSendData, participants_c
embed.set_footer(text="Ends at")
return embed

async def send_gaw(self, channel: discord.TextChannel, data: GiveawayToSendData):
async def send_gaw(self, channel: AcceptableChannelType, data: GiveawayToSendData):
"Send a giveaway message in a given channel"
embed = await self.create_active_gaw_embed(data)
view = GiveawayView(self.bot, data, "Join the giveaway!")
Expand All @@ -229,7 +236,7 @@ async def send_gaw(self, channel: discord.TextChannel, data: GiveawayToSendData)
async def fetch_gaw_message(self, data: GiveawayData):
"Fetch the Discord message for a giveaway"
channel = self.bot.get_channel(data["channel"])
if not isinstance(channel, discord.TextChannel):
if not isinstance(channel, AcceptableChannel):
return None
try:
message = await channel.fetch_message(data["message"])
Expand Down

0 comments on commit bc213d3

Please sign in to comment.