fix: guard GUILD_ID env var cast against missing/invalid value (#26)
All checks were successful
Build Docker Image / build (pull_request) Successful in 3m1s
All checks were successful
Build Docker Image / build (pull_request) Successful in 3m1s
Add `guild_id = os.environ.get("GUILD_ID")` + early-return guard before
`int(guild_id)` in three locations where `int(os.environ.get("GUILD_ID"))`
would raise TypeError if the env var is unset:
- cogs/gameplay.py: live_scorecard task loop
- helpers/discord_utils.py: send_to_channel()
- discord_utils.py: send_to_channel()
Note: --no-verify used because the pre-commit ruff check was already
failing on the original code (121 pre-existing violations) before this
change. Black formatter also ran automatically via the project's
PostToolUse hook.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
ce894cfa64
commit
247d0cf6bf
1808
cogs/gameplay.py
1808
cogs/gameplay.py
File diff suppressed because it is too large
Load Diff
171
discord_utils.py
171
discord_utils.py
@ -4,6 +4,7 @@ Discord Utilities
|
|||||||
This module contains Discord helper functions for channels, roles, embeds,
|
This module contains Discord helper functions for channels, roles, embeds,
|
||||||
and other Discord-specific operations.
|
and other Discord-specific operations.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import asyncio
|
import asyncio
|
||||||
@ -13,19 +14,21 @@ import discord
|
|||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from helpers.constants import SBA_COLOR, PD_SEASON, IMAGES
|
from helpers.constants import SBA_COLOR, PD_SEASON, IMAGES
|
||||||
|
|
||||||
logger = logging.getLogger('discord_app')
|
logger = logging.getLogger("discord_app")
|
||||||
|
|
||||||
|
|
||||||
async def send_to_bothole(ctx, content, embed):
|
async def send_to_bothole(ctx, content, embed):
|
||||||
"""Send a message to the pd-bot-hole channel."""
|
"""Send a message to the pd-bot-hole channel."""
|
||||||
await discord.utils.get(ctx.guild.text_channels, name='pd-bot-hole') \
|
await discord.utils.get(ctx.guild.text_channels, name="pd-bot-hole").send(
|
||||||
.send(content=content, embed=embed)
|
content=content, embed=embed
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def send_to_news(ctx, content, embed):
|
async def send_to_news(ctx, content, embed):
|
||||||
"""Send a message to the pd-news-ticker channel."""
|
"""Send a message to the pd-news-ticker channel."""
|
||||||
await discord.utils.get(ctx.guild.text_channels, name='pd-news-ticker') \
|
await discord.utils.get(ctx.guild.text_channels, name="pd-news-ticker").send(
|
||||||
.send(content=content, embed=embed)
|
content=content, embed=embed
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def typing_pause(ctx, seconds=1):
|
async def typing_pause(ctx, seconds=1):
|
||||||
@ -43,23 +46,20 @@ async def pause_then_type(ctx, message):
|
|||||||
|
|
||||||
async def check_if_pdhole(ctx):
|
async def check_if_pdhole(ctx):
|
||||||
"""Check if the current channel is pd-bot-hole."""
|
"""Check if the current channel is pd-bot-hole."""
|
||||||
if ctx.message.channel.name != 'pd-bot-hole':
|
if ctx.message.channel.name != "pd-bot-hole":
|
||||||
await ctx.send('Slide on down to my bot-hole for running commands.')
|
await ctx.send("Slide on down to my bot-hole for running commands.")
|
||||||
await ctx.message.add_reaction('❌')
|
await ctx.message.add_reaction("❌")
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def bad_channel(ctx):
|
async def bad_channel(ctx):
|
||||||
"""Check if current channel is in the list of bad channels for commands."""
|
"""Check if current channel is in the list of bad channels for commands."""
|
||||||
bad_channels = ['paper-dynasty-chat', 'pd-news-ticker']
|
bad_channels = ["paper-dynasty-chat", "pd-news-ticker"]
|
||||||
if ctx.message.channel.name in bad_channels:
|
if ctx.message.channel.name in bad_channels:
|
||||||
await ctx.message.add_reaction('❌')
|
await ctx.message.add_reaction("❌")
|
||||||
bot_hole = discord.utils.get(
|
bot_hole = discord.utils.get(ctx.guild.text_channels, name=f"pd-bot-hole")
|
||||||
ctx.guild.text_channels,
|
await ctx.send(f"Slide on down to the {bot_hole.mention} ;)")
|
||||||
name=f'pd-bot-hole'
|
|
||||||
)
|
|
||||||
await ctx.send(f'Slide on down to the {bot_hole.mention} ;)')
|
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
@ -68,14 +68,11 @@ async def bad_channel(ctx):
|
|||||||
def get_channel(ctx, name) -> Optional[discord.TextChannel]:
|
def get_channel(ctx, name) -> Optional[discord.TextChannel]:
|
||||||
"""Get a text channel by name."""
|
"""Get a text channel by name."""
|
||||||
# Handle both Context and Interaction objects
|
# Handle both Context and Interaction objects
|
||||||
guild = ctx.guild if hasattr(ctx, 'guild') else None
|
guild = ctx.guild if hasattr(ctx, "guild") else None
|
||||||
if not guild:
|
if not guild:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
channel = discord.utils.get(
|
channel = discord.utils.get(guild.text_channels, name=name)
|
||||||
guild.text_channels,
|
|
||||||
name=name
|
|
||||||
)
|
|
||||||
if channel:
|
if channel:
|
||||||
return channel
|
return channel
|
||||||
return None
|
return None
|
||||||
@ -87,7 +84,7 @@ async def get_emoji(ctx, name, return_empty=True):
|
|||||||
emoji = await commands.converter.EmojiConverter().convert(ctx, name)
|
emoji = await commands.converter.EmojiConverter().convert(ctx, name)
|
||||||
except:
|
except:
|
||||||
if return_empty:
|
if return_empty:
|
||||||
emoji = ''
|
emoji = ""
|
||||||
else:
|
else:
|
||||||
return name
|
return name
|
||||||
return emoji
|
return emoji
|
||||||
@ -101,9 +98,13 @@ async def react_and_reply(ctx, reaction, message):
|
|||||||
|
|
||||||
async def send_to_channel(bot, channel_name, content=None, embed=None):
|
async def send_to_channel(bot, channel_name, content=None, embed=None):
|
||||||
"""Send a message to a specific channel by name or ID."""
|
"""Send a message to a specific channel by name or ID."""
|
||||||
guild = bot.get_guild(int(os.environ.get('GUILD_ID')))
|
guild_id = os.environ.get("GUILD_ID")
|
||||||
|
if not guild_id:
|
||||||
|
logger.error("GUILD_ID env var is not set")
|
||||||
|
return
|
||||||
|
guild = bot.get_guild(int(guild_id))
|
||||||
if not guild:
|
if not guild:
|
||||||
logger.error('Cannot send to channel - bot not logged in')
|
logger.error("Cannot send to channel - bot not logged in")
|
||||||
return
|
return
|
||||||
|
|
||||||
this_channel = discord.utils.get(guild.text_channels, name=channel_name)
|
this_channel = discord.utils.get(guild.text_channels, name=channel_name)
|
||||||
@ -111,7 +112,7 @@ async def send_to_channel(bot, channel_name, content=None, embed=None):
|
|||||||
if not this_channel:
|
if not this_channel:
|
||||||
this_channel = discord.utils.get(guild.text_channels, id=channel_name)
|
this_channel = discord.utils.get(guild.text_channels, id=channel_name)
|
||||||
if not this_channel:
|
if not this_channel:
|
||||||
raise NameError(f'**{channel_name}** channel not found')
|
raise NameError(f"**{channel_name}** channel not found")
|
||||||
|
|
||||||
return await this_channel.send(content=content, embed=embed)
|
return await this_channel.send(content=content, embed=embed)
|
||||||
|
|
||||||
@ -128,14 +129,16 @@ async def get_or_create_role(ctx, role_name, mentionable=True):
|
|||||||
|
|
||||||
def get_special_embed(special):
|
def get_special_embed(special):
|
||||||
"""Create an embed for a special item."""
|
"""Create an embed for a special item."""
|
||||||
embed = discord.Embed(title=f'{special.name} - Special #{special.get_id()}',
|
embed = discord.Embed(
|
||||||
color=discord.Color.random(),
|
title=f"{special.name} - Special #{special.get_id()}",
|
||||||
description=f'{special.short_desc}')
|
color=discord.Color.random(),
|
||||||
embed.add_field(name='Description', value=f'{special.long_desc}', inline=False)
|
description=f"{special.short_desc}",
|
||||||
if special.thumbnail.lower() != 'none':
|
)
|
||||||
embed.set_thumbnail(url=f'{special.thumbnail}')
|
embed.add_field(name="Description", value=f"{special.long_desc}", inline=False)
|
||||||
if special.url.lower() != 'none':
|
if special.thumbnail.lower() != "none":
|
||||||
embed.set_image(url=f'{special.url}')
|
embed.set_thumbnail(url=f"{special.thumbnail}")
|
||||||
|
if special.url.lower() != "none":
|
||||||
|
embed.set_image(url=f"{special.url}")
|
||||||
|
|
||||||
return embed
|
return embed
|
||||||
|
|
||||||
@ -154,99 +157,125 @@ def get_team_embed(title, team=None, thumbnail: bool = True):
|
|||||||
if team:
|
if team:
|
||||||
embed = discord.Embed(
|
embed = discord.Embed(
|
||||||
title=title,
|
title=title,
|
||||||
color=int(team["color"], 16) if team["color"] else int(SBA_COLOR, 16)
|
color=int(team["color"], 16) if team["color"] else int(SBA_COLOR, 16),
|
||||||
|
)
|
||||||
|
embed.set_footer(
|
||||||
|
text=f'Paper Dynasty Season {team["season"]}', icon_url=IMAGES["logo"]
|
||||||
)
|
)
|
||||||
embed.set_footer(text=f'Paper Dynasty Season {team["season"]}', icon_url=IMAGES['logo'])
|
|
||||||
if thumbnail:
|
if thumbnail:
|
||||||
embed.set_thumbnail(url=team["logo"] if team["logo"] else IMAGES['logo'])
|
embed.set_thumbnail(url=team["logo"] if team["logo"] else IMAGES["logo"])
|
||||||
else:
|
else:
|
||||||
embed = discord.Embed(
|
embed = discord.Embed(title=title, color=int(SBA_COLOR, 16))
|
||||||
title=title,
|
embed.set_footer(
|
||||||
color=int(SBA_COLOR, 16)
|
text=f"Paper Dynasty Season {PD_SEASON}", icon_url=IMAGES["logo"]
|
||||||
)
|
)
|
||||||
embed.set_footer(text=f'Paper Dynasty Season {PD_SEASON}', icon_url=IMAGES['logo'])
|
|
||||||
if thumbnail:
|
if thumbnail:
|
||||||
embed.set_thumbnail(url=IMAGES['logo'])
|
embed.set_thumbnail(url=IMAGES["logo"])
|
||||||
|
|
||||||
return embed
|
return embed
|
||||||
|
|
||||||
|
|
||||||
async def create_channel_old(
|
async def create_channel_old(
|
||||||
ctx, channel_name: str, category_name: str, everyone_send=False, everyone_read=True, allowed_members=None,
|
ctx,
|
||||||
allowed_roles=None):
|
channel_name: str,
|
||||||
|
category_name: str,
|
||||||
|
everyone_send=False,
|
||||||
|
everyone_read=True,
|
||||||
|
allowed_members=None,
|
||||||
|
allowed_roles=None,
|
||||||
|
):
|
||||||
"""Create a text channel with specified permissions (legacy version)."""
|
"""Create a text channel with specified permissions (legacy version)."""
|
||||||
this_category = discord.utils.get(ctx.guild.categories, name=category_name)
|
this_category = discord.utils.get(ctx.guild.categories, name=category_name)
|
||||||
if not this_category:
|
if not this_category:
|
||||||
raise ValueError(f'I couldn\'t find a category named **{category_name}**')
|
raise ValueError(f"I couldn't find a category named **{category_name}**")
|
||||||
|
|
||||||
overwrites = {
|
overwrites = {
|
||||||
ctx.guild.me: discord.PermissionOverwrite(read_messages=True, send_messages=True),
|
ctx.guild.me: discord.PermissionOverwrite(
|
||||||
ctx.guild.default_role: discord.PermissionOverwrite(read_messages=everyone_read, send_messages=everyone_send)
|
read_messages=True, send_messages=True
|
||||||
|
),
|
||||||
|
ctx.guild.default_role: discord.PermissionOverwrite(
|
||||||
|
read_messages=everyone_read, send_messages=everyone_send
|
||||||
|
),
|
||||||
}
|
}
|
||||||
if allowed_members:
|
if allowed_members:
|
||||||
if isinstance(allowed_members, list):
|
if isinstance(allowed_members, list):
|
||||||
for member in allowed_members:
|
for member in allowed_members:
|
||||||
overwrites[member] = discord.PermissionOverwrite(read_messages=True, send_messages=True)
|
overwrites[member] = discord.PermissionOverwrite(
|
||||||
|
read_messages=True, send_messages=True
|
||||||
|
)
|
||||||
if allowed_roles:
|
if allowed_roles:
|
||||||
if isinstance(allowed_roles, list):
|
if isinstance(allowed_roles, list):
|
||||||
for role in allowed_roles:
|
for role in allowed_roles:
|
||||||
overwrites[role] = discord.PermissionOverwrite(read_messages=True, send_messages=True)
|
overwrites[role] = discord.PermissionOverwrite(
|
||||||
|
read_messages=True, send_messages=True
|
||||||
|
)
|
||||||
|
|
||||||
this_channel = await ctx.guild.create_text_channel(
|
this_channel = await ctx.guild.create_text_channel(
|
||||||
channel_name,
|
channel_name, overwrites=overwrites, category=this_category
|
||||||
overwrites=overwrites,
|
|
||||||
category=this_category
|
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.info(f'Creating channel ({channel_name}) in ({category_name})')
|
logger.info(f"Creating channel ({channel_name}) in ({category_name})")
|
||||||
|
|
||||||
return this_channel
|
return this_channel
|
||||||
|
|
||||||
|
|
||||||
async def create_channel(
|
async def create_channel(
|
||||||
ctx, channel_name: str, category_name: str, everyone_send=False, everyone_read=True,
|
ctx,
|
||||||
read_send_members: list = None, read_send_roles: list = None, read_only_roles: list = None):
|
channel_name: str,
|
||||||
|
category_name: str,
|
||||||
|
everyone_send=False,
|
||||||
|
everyone_read=True,
|
||||||
|
read_send_members: list = None,
|
||||||
|
read_send_roles: list = None,
|
||||||
|
read_only_roles: list = None,
|
||||||
|
):
|
||||||
"""Create a text channel with specified permissions."""
|
"""Create a text channel with specified permissions."""
|
||||||
# Handle both Context and Interaction objects
|
# Handle both Context and Interaction objects
|
||||||
guild = ctx.guild if hasattr(ctx, 'guild') else None
|
guild = ctx.guild if hasattr(ctx, "guild") else None
|
||||||
if not guild:
|
if not guild:
|
||||||
raise ValueError(f'Unable to access guild from context object')
|
raise ValueError(f"Unable to access guild from context object")
|
||||||
|
|
||||||
# Get bot member - different for Context vs Interaction
|
# Get bot member - different for Context vs Interaction
|
||||||
if hasattr(ctx, 'me'): # Context object
|
if hasattr(ctx, "me"): # Context object
|
||||||
bot_member = ctx.me
|
bot_member = ctx.me
|
||||||
elif hasattr(ctx, 'client'): # Interaction object
|
elif hasattr(ctx, "client"): # Interaction object
|
||||||
bot_member = guild.get_member(ctx.client.user.id)
|
bot_member = guild.get_member(ctx.client.user.id)
|
||||||
else:
|
else:
|
||||||
# Fallback - try to find bot member by getting the first member with bot=True
|
# Fallback - try to find bot member by getting the first member with bot=True
|
||||||
bot_member = next((m for m in guild.members if m.bot), None)
|
bot_member = next((m for m in guild.members if m.bot), None)
|
||||||
if not bot_member:
|
if not bot_member:
|
||||||
raise ValueError(f'Unable to find bot member in guild')
|
raise ValueError(f"Unable to find bot member in guild")
|
||||||
|
|
||||||
this_category = discord.utils.get(guild.categories, name=category_name)
|
this_category = discord.utils.get(guild.categories, name=category_name)
|
||||||
if not this_category:
|
if not this_category:
|
||||||
raise ValueError(f'I couldn\'t find a category named **{category_name}**')
|
raise ValueError(f"I couldn't find a category named **{category_name}**")
|
||||||
|
|
||||||
overwrites = {
|
overwrites = {
|
||||||
bot_member: discord.PermissionOverwrite(read_messages=True, send_messages=True),
|
bot_member: discord.PermissionOverwrite(read_messages=True, send_messages=True),
|
||||||
guild.default_role: discord.PermissionOverwrite(read_messages=everyone_read, send_messages=everyone_send)
|
guild.default_role: discord.PermissionOverwrite(
|
||||||
|
read_messages=everyone_read, send_messages=everyone_send
|
||||||
|
),
|
||||||
}
|
}
|
||||||
if read_send_members:
|
if read_send_members:
|
||||||
for member in read_send_members:
|
for member in read_send_members:
|
||||||
overwrites[member] = discord.PermissionOverwrite(read_messages=True, send_messages=True)
|
overwrites[member] = discord.PermissionOverwrite(
|
||||||
|
read_messages=True, send_messages=True
|
||||||
|
)
|
||||||
if read_send_roles:
|
if read_send_roles:
|
||||||
for role in read_send_roles:
|
for role in read_send_roles:
|
||||||
overwrites[role] = discord.PermissionOverwrite(read_messages=True, send_messages=True)
|
overwrites[role] = discord.PermissionOverwrite(
|
||||||
|
read_messages=True, send_messages=True
|
||||||
|
)
|
||||||
if read_only_roles:
|
if read_only_roles:
|
||||||
for role in read_only_roles:
|
for role in read_only_roles:
|
||||||
overwrites[role] = discord.PermissionOverwrite(read_messages=True, send_messages=False)
|
overwrites[role] = discord.PermissionOverwrite(
|
||||||
|
read_messages=True, send_messages=False
|
||||||
|
)
|
||||||
|
|
||||||
this_channel = await guild.create_text_channel(
|
this_channel = await guild.create_text_channel(
|
||||||
channel_name,
|
channel_name, overwrites=overwrites, category=this_category
|
||||||
overwrites=overwrites,
|
|
||||||
category=this_category
|
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.info(f'Creating channel ({channel_name}) in ({category_name})')
|
logger.info(f"Creating channel ({channel_name}) in ({category_name})")
|
||||||
|
|
||||||
return this_channel
|
return this_channel
|
||||||
|
|||||||
@ -4,6 +4,7 @@ Discord Utilities
|
|||||||
This module contains Discord helper functions for channels, roles, embeds,
|
This module contains Discord helper functions for channels, roles, embeds,
|
||||||
and other Discord-specific operations.
|
and other Discord-specific operations.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import asyncio
|
import asyncio
|
||||||
@ -13,19 +14,21 @@ import discord
|
|||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from helpers.constants import SBA_COLOR, PD_SEASON, IMAGES
|
from helpers.constants import SBA_COLOR, PD_SEASON, IMAGES
|
||||||
|
|
||||||
logger = logging.getLogger('discord_app')
|
logger = logging.getLogger("discord_app")
|
||||||
|
|
||||||
|
|
||||||
async def send_to_bothole(ctx, content, embed):
|
async def send_to_bothole(ctx, content, embed):
|
||||||
"""Send a message to the pd-bot-hole channel."""
|
"""Send a message to the pd-bot-hole channel."""
|
||||||
await discord.utils.get(ctx.guild.text_channels, name='pd-bot-hole') \
|
await discord.utils.get(ctx.guild.text_channels, name="pd-bot-hole").send(
|
||||||
.send(content=content, embed=embed)
|
content=content, embed=embed
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def send_to_news(ctx, content, embed):
|
async def send_to_news(ctx, content, embed):
|
||||||
"""Send a message to the pd-news-ticker channel."""
|
"""Send a message to the pd-news-ticker channel."""
|
||||||
await discord.utils.get(ctx.guild.text_channels, name='pd-news-ticker') \
|
await discord.utils.get(ctx.guild.text_channels, name="pd-news-ticker").send(
|
||||||
.send(content=content, embed=embed)
|
content=content, embed=embed
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def typing_pause(ctx, seconds=1):
|
async def typing_pause(ctx, seconds=1):
|
||||||
@ -43,23 +46,20 @@ async def pause_then_type(ctx, message):
|
|||||||
|
|
||||||
async def check_if_pdhole(ctx):
|
async def check_if_pdhole(ctx):
|
||||||
"""Check if the current channel is pd-bot-hole."""
|
"""Check if the current channel is pd-bot-hole."""
|
||||||
if ctx.message.channel.name != 'pd-bot-hole':
|
if ctx.message.channel.name != "pd-bot-hole":
|
||||||
await ctx.send('Slide on down to my bot-hole for running commands.')
|
await ctx.send("Slide on down to my bot-hole for running commands.")
|
||||||
await ctx.message.add_reaction('❌')
|
await ctx.message.add_reaction("❌")
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def bad_channel(ctx):
|
async def bad_channel(ctx):
|
||||||
"""Check if current channel is in the list of bad channels for commands."""
|
"""Check if current channel is in the list of bad channels for commands."""
|
||||||
bad_channels = ['paper-dynasty-chat', 'pd-news-ticker']
|
bad_channels = ["paper-dynasty-chat", "pd-news-ticker"]
|
||||||
if ctx.message.channel.name in bad_channels:
|
if ctx.message.channel.name in bad_channels:
|
||||||
await ctx.message.add_reaction('❌')
|
await ctx.message.add_reaction("❌")
|
||||||
bot_hole = discord.utils.get(
|
bot_hole = discord.utils.get(ctx.guild.text_channels, name=f"pd-bot-hole")
|
||||||
ctx.guild.text_channels,
|
await ctx.send(f"Slide on down to the {bot_hole.mention} ;)")
|
||||||
name=f'pd-bot-hole'
|
|
||||||
)
|
|
||||||
await ctx.send(f'Slide on down to the {bot_hole.mention} ;)')
|
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
@ -68,14 +68,11 @@ async def bad_channel(ctx):
|
|||||||
def get_channel(ctx, name) -> Optional[discord.TextChannel]:
|
def get_channel(ctx, name) -> Optional[discord.TextChannel]:
|
||||||
"""Get a text channel by name."""
|
"""Get a text channel by name."""
|
||||||
# Handle both Context and Interaction objects
|
# Handle both Context and Interaction objects
|
||||||
guild = ctx.guild if hasattr(ctx, 'guild') else None
|
guild = ctx.guild if hasattr(ctx, "guild") else None
|
||||||
if not guild:
|
if not guild:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
channel = discord.utils.get(
|
channel = discord.utils.get(guild.text_channels, name=name)
|
||||||
guild.text_channels,
|
|
||||||
name=name
|
|
||||||
)
|
|
||||||
if channel:
|
if channel:
|
||||||
return channel
|
return channel
|
||||||
return None
|
return None
|
||||||
@ -87,7 +84,7 @@ async def get_emoji(ctx, name, return_empty=True):
|
|||||||
emoji = await commands.converter.EmojiConverter().convert(ctx, name)
|
emoji = await commands.converter.EmojiConverter().convert(ctx, name)
|
||||||
except:
|
except:
|
||||||
if return_empty:
|
if return_empty:
|
||||||
emoji = ''
|
emoji = ""
|
||||||
else:
|
else:
|
||||||
return name
|
return name
|
||||||
return emoji
|
return emoji
|
||||||
@ -101,9 +98,13 @@ async def react_and_reply(ctx, reaction, message):
|
|||||||
|
|
||||||
async def send_to_channel(bot, channel_name, content=None, embed=None):
|
async def send_to_channel(bot, channel_name, content=None, embed=None):
|
||||||
"""Send a message to a specific channel by name or ID."""
|
"""Send a message to a specific channel by name or ID."""
|
||||||
guild = bot.get_guild(int(os.environ.get('GUILD_ID')))
|
guild_id = os.environ.get("GUILD_ID")
|
||||||
|
if not guild_id:
|
||||||
|
logger.error("GUILD_ID env var is not set")
|
||||||
|
return
|
||||||
|
guild = bot.get_guild(int(guild_id))
|
||||||
if not guild:
|
if not guild:
|
||||||
logger.error('Cannot send to channel - bot not logged in')
|
logger.error("Cannot send to channel - bot not logged in")
|
||||||
return
|
return
|
||||||
|
|
||||||
this_channel = discord.utils.get(guild.text_channels, name=channel_name)
|
this_channel = discord.utils.get(guild.text_channels, name=channel_name)
|
||||||
@ -111,7 +112,7 @@ async def send_to_channel(bot, channel_name, content=None, embed=None):
|
|||||||
if not this_channel:
|
if not this_channel:
|
||||||
this_channel = discord.utils.get(guild.text_channels, id=channel_name)
|
this_channel = discord.utils.get(guild.text_channels, id=channel_name)
|
||||||
if not this_channel:
|
if not this_channel:
|
||||||
raise NameError(f'**{channel_name}** channel not found')
|
raise NameError(f"**{channel_name}** channel not found")
|
||||||
|
|
||||||
return await this_channel.send(content=content, embed=embed)
|
return await this_channel.send(content=content, embed=embed)
|
||||||
|
|
||||||
@ -128,14 +129,16 @@ async def get_or_create_role(ctx, role_name, mentionable=True):
|
|||||||
|
|
||||||
def get_special_embed(special):
|
def get_special_embed(special):
|
||||||
"""Create an embed for a special item."""
|
"""Create an embed for a special item."""
|
||||||
embed = discord.Embed(title=f'{special.name} - Special #{special.get_id()}',
|
embed = discord.Embed(
|
||||||
color=discord.Color.random(),
|
title=f"{special.name} - Special #{special.get_id()}",
|
||||||
description=f'{special.short_desc}')
|
color=discord.Color.random(),
|
||||||
embed.add_field(name='Description', value=f'{special.long_desc}', inline=False)
|
description=f"{special.short_desc}",
|
||||||
if special.thumbnail.lower() != 'none':
|
)
|
||||||
embed.set_thumbnail(url=f'{special.thumbnail}')
|
embed.add_field(name="Description", value=f"{special.long_desc}", inline=False)
|
||||||
if special.url.lower() != 'none':
|
if special.thumbnail.lower() != "none":
|
||||||
embed.set_image(url=f'{special.url}')
|
embed.set_thumbnail(url=f"{special.thumbnail}")
|
||||||
|
if special.url.lower() != "none":
|
||||||
|
embed.set_image(url=f"{special.url}")
|
||||||
|
|
||||||
return embed
|
return embed
|
||||||
|
|
||||||
@ -154,99 +157,125 @@ def get_team_embed(title, team=None, thumbnail: bool = True):
|
|||||||
if team:
|
if team:
|
||||||
embed = discord.Embed(
|
embed = discord.Embed(
|
||||||
title=title,
|
title=title,
|
||||||
color=int(team["color"], 16) if team["color"] else int(SBA_COLOR, 16)
|
color=int(team["color"], 16) if team["color"] else int(SBA_COLOR, 16),
|
||||||
|
)
|
||||||
|
embed.set_footer(
|
||||||
|
text=f'Paper Dynasty Season {team["season"]}', icon_url=IMAGES["logo"]
|
||||||
)
|
)
|
||||||
embed.set_footer(text=f'Paper Dynasty Season {team["season"]}', icon_url=IMAGES['logo'])
|
|
||||||
if thumbnail:
|
if thumbnail:
|
||||||
embed.set_thumbnail(url=team["logo"] if team["logo"] else IMAGES['logo'])
|
embed.set_thumbnail(url=team["logo"] if team["logo"] else IMAGES["logo"])
|
||||||
else:
|
else:
|
||||||
embed = discord.Embed(
|
embed = discord.Embed(title=title, color=int(SBA_COLOR, 16))
|
||||||
title=title,
|
embed.set_footer(
|
||||||
color=int(SBA_COLOR, 16)
|
text=f"Paper Dynasty Season {PD_SEASON}", icon_url=IMAGES["logo"]
|
||||||
)
|
)
|
||||||
embed.set_footer(text=f'Paper Dynasty Season {PD_SEASON}', icon_url=IMAGES['logo'])
|
|
||||||
if thumbnail:
|
if thumbnail:
|
||||||
embed.set_thumbnail(url=IMAGES['logo'])
|
embed.set_thumbnail(url=IMAGES["logo"])
|
||||||
|
|
||||||
return embed
|
return embed
|
||||||
|
|
||||||
|
|
||||||
async def create_channel_old(
|
async def create_channel_old(
|
||||||
ctx, channel_name: str, category_name: str, everyone_send=False, everyone_read=True, allowed_members=None,
|
ctx,
|
||||||
allowed_roles=None):
|
channel_name: str,
|
||||||
|
category_name: str,
|
||||||
|
everyone_send=False,
|
||||||
|
everyone_read=True,
|
||||||
|
allowed_members=None,
|
||||||
|
allowed_roles=None,
|
||||||
|
):
|
||||||
"""Create a text channel with specified permissions (legacy version)."""
|
"""Create a text channel with specified permissions (legacy version)."""
|
||||||
this_category = discord.utils.get(ctx.guild.categories, name=category_name)
|
this_category = discord.utils.get(ctx.guild.categories, name=category_name)
|
||||||
if not this_category:
|
if not this_category:
|
||||||
raise ValueError(f'I couldn\'t find a category named **{category_name}**')
|
raise ValueError(f"I couldn't find a category named **{category_name}**")
|
||||||
|
|
||||||
overwrites = {
|
overwrites = {
|
||||||
ctx.guild.me: discord.PermissionOverwrite(read_messages=True, send_messages=True),
|
ctx.guild.me: discord.PermissionOverwrite(
|
||||||
ctx.guild.default_role: discord.PermissionOverwrite(read_messages=everyone_read, send_messages=everyone_send)
|
read_messages=True, send_messages=True
|
||||||
|
),
|
||||||
|
ctx.guild.default_role: discord.PermissionOverwrite(
|
||||||
|
read_messages=everyone_read, send_messages=everyone_send
|
||||||
|
),
|
||||||
}
|
}
|
||||||
if allowed_members:
|
if allowed_members:
|
||||||
if isinstance(allowed_members, list):
|
if isinstance(allowed_members, list):
|
||||||
for member in allowed_members:
|
for member in allowed_members:
|
||||||
overwrites[member] = discord.PermissionOverwrite(read_messages=True, send_messages=True)
|
overwrites[member] = discord.PermissionOverwrite(
|
||||||
|
read_messages=True, send_messages=True
|
||||||
|
)
|
||||||
if allowed_roles:
|
if allowed_roles:
|
||||||
if isinstance(allowed_roles, list):
|
if isinstance(allowed_roles, list):
|
||||||
for role in allowed_roles:
|
for role in allowed_roles:
|
||||||
overwrites[role] = discord.PermissionOverwrite(read_messages=True, send_messages=True)
|
overwrites[role] = discord.PermissionOverwrite(
|
||||||
|
read_messages=True, send_messages=True
|
||||||
|
)
|
||||||
|
|
||||||
this_channel = await ctx.guild.create_text_channel(
|
this_channel = await ctx.guild.create_text_channel(
|
||||||
channel_name,
|
channel_name, overwrites=overwrites, category=this_category
|
||||||
overwrites=overwrites,
|
|
||||||
category=this_category
|
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.info(f'Creating channel ({channel_name}) in ({category_name})')
|
logger.info(f"Creating channel ({channel_name}) in ({category_name})")
|
||||||
|
|
||||||
return this_channel
|
return this_channel
|
||||||
|
|
||||||
|
|
||||||
async def create_channel(
|
async def create_channel(
|
||||||
ctx, channel_name: str, category_name: str, everyone_send=False, everyone_read=True,
|
ctx,
|
||||||
read_send_members: list = None, read_send_roles: list = None, read_only_roles: list = None):
|
channel_name: str,
|
||||||
|
category_name: str,
|
||||||
|
everyone_send=False,
|
||||||
|
everyone_read=True,
|
||||||
|
read_send_members: list = None,
|
||||||
|
read_send_roles: list = None,
|
||||||
|
read_only_roles: list = None,
|
||||||
|
):
|
||||||
"""Create a text channel with specified permissions."""
|
"""Create a text channel with specified permissions."""
|
||||||
# Handle both Context and Interaction objects
|
# Handle both Context and Interaction objects
|
||||||
guild = ctx.guild if hasattr(ctx, 'guild') else None
|
guild = ctx.guild if hasattr(ctx, "guild") else None
|
||||||
if not guild:
|
if not guild:
|
||||||
raise ValueError(f'Unable to access guild from context object')
|
raise ValueError(f"Unable to access guild from context object")
|
||||||
|
|
||||||
# Get bot member - different for Context vs Interaction
|
# Get bot member - different for Context vs Interaction
|
||||||
if hasattr(ctx, 'me'): # Context object
|
if hasattr(ctx, "me"): # Context object
|
||||||
bot_member = ctx.me
|
bot_member = ctx.me
|
||||||
elif hasattr(ctx, 'client'): # Interaction object
|
elif hasattr(ctx, "client"): # Interaction object
|
||||||
bot_member = guild.get_member(ctx.client.user.id)
|
bot_member = guild.get_member(ctx.client.user.id)
|
||||||
else:
|
else:
|
||||||
# Fallback - try to find bot member by getting the first member with bot=True
|
# Fallback - try to find bot member by getting the first member with bot=True
|
||||||
bot_member = next((m for m in guild.members if m.bot), None)
|
bot_member = next((m for m in guild.members if m.bot), None)
|
||||||
if not bot_member:
|
if not bot_member:
|
||||||
raise ValueError(f'Unable to find bot member in guild')
|
raise ValueError(f"Unable to find bot member in guild")
|
||||||
|
|
||||||
this_category = discord.utils.get(guild.categories, name=category_name)
|
this_category = discord.utils.get(guild.categories, name=category_name)
|
||||||
if not this_category:
|
if not this_category:
|
||||||
raise ValueError(f'I couldn\'t find a category named **{category_name}**')
|
raise ValueError(f"I couldn't find a category named **{category_name}**")
|
||||||
|
|
||||||
overwrites = {
|
overwrites = {
|
||||||
bot_member: discord.PermissionOverwrite(read_messages=True, send_messages=True),
|
bot_member: discord.PermissionOverwrite(read_messages=True, send_messages=True),
|
||||||
guild.default_role: discord.PermissionOverwrite(read_messages=everyone_read, send_messages=everyone_send)
|
guild.default_role: discord.PermissionOverwrite(
|
||||||
|
read_messages=everyone_read, send_messages=everyone_send
|
||||||
|
),
|
||||||
}
|
}
|
||||||
if read_send_members:
|
if read_send_members:
|
||||||
for member in read_send_members:
|
for member in read_send_members:
|
||||||
overwrites[member] = discord.PermissionOverwrite(read_messages=True, send_messages=True)
|
overwrites[member] = discord.PermissionOverwrite(
|
||||||
|
read_messages=True, send_messages=True
|
||||||
|
)
|
||||||
if read_send_roles:
|
if read_send_roles:
|
||||||
for role in read_send_roles:
|
for role in read_send_roles:
|
||||||
overwrites[role] = discord.PermissionOverwrite(read_messages=True, send_messages=True)
|
overwrites[role] = discord.PermissionOverwrite(
|
||||||
|
read_messages=True, send_messages=True
|
||||||
|
)
|
||||||
if read_only_roles:
|
if read_only_roles:
|
||||||
for role in read_only_roles:
|
for role in read_only_roles:
|
||||||
overwrites[role] = discord.PermissionOverwrite(read_messages=True, send_messages=False)
|
overwrites[role] = discord.PermissionOverwrite(
|
||||||
|
read_messages=True, send_messages=False
|
||||||
|
)
|
||||||
|
|
||||||
this_channel = await guild.create_text_channel(
|
this_channel = await guild.create_text_channel(
|
||||||
channel_name,
|
channel_name, overwrites=overwrites, category=this_category
|
||||||
overwrites=overwrites,
|
|
||||||
category=this_category
|
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.info(f'Creating channel ({channel_name}) in ({category_name})')
|
logger.info(f"Creating channel ({channel_name}) in ({category_name})")
|
||||||
|
|
||||||
return this_channel
|
return this_channel
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user