Skip to content

Commit

Permalink
Try again?
Browse files Browse the repository at this point in the history
  • Loading branch information
camerongraybill committed Sep 15, 2024
1 parent 99819f9 commit 9117539
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/discord_bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from discord import Intents, Activity, ActivityType
from discord.ext.commands import Bot

from easy_messages.add_commands import add_easy_commands
from . import settings


Expand All @@ -17,4 +18,5 @@ async def build_bot() -> Bot:
)
for cog_cls in settings.COGS:
await b.add_cog(cog_cls(b))
add_easy_commands(b)
return b
24 changes: 24 additions & 0 deletions src/easy_messages/add_commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from typing import Any, Sequence

from discord.app_commands import Command
from discord.ext import commands
from discord.ext.commands import Bot, Context

from discord_bot.checks import is_in_channel
from . import settings

def _build_command(name: str, channels: set[str] | None, response: Sequence[str]) -> Command[Any, Any, Any]:
async def _(ctx: Context[Bot]) -> None:
for resp in response:
await ctx.send(resp)

f = _
f.__name__ = name
if channels:
f = is_in_channel(channels)(f)

return commands.command(name=name)(f)

def add_easy_commands(bot: Bot) -> None:
for command in settings.COMMANDS:
bot.add_command(_build_command(*command))
13 changes: 6 additions & 7 deletions src/easy_messages/cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@


class EasyCog(BaseCog):
def __new__(cls, bot: Bot) -> Any:
for cmd in settings.COMMANDS:
cls.__cog_commands__.append(build_command(*cmd))
return super().__new__(cls, bot)

""" Attributes are dynamically defined on import """

def build_command(
name: str, channels: Optional[set[str]], response: Sequence[str]
) -> Command[EasyCog, Any, Any]:
) -> None:
async def _(self: EasyCog, ctx: Context[Bot]) -> None:
for resp in response:
await ctx.send(resp)
Expand All @@ -31,4 +27,7 @@ async def _(self: EasyCog, ctx: Context[Bot]) -> None:
if channels:
f = is_in_channel(channels)(f)

return commands.command(name=name)(f)
setattr(EasyCog, name, commands.command(name=name)(f))

for cmd in settings.COMMANDS:
build_command(*cmd)
1 change: 0 additions & 1 deletion src/gam/settings/_gam.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@

DISCORD_COGS = [
"lmgtfy.cog.LMGTFYCog",
"easy_messages.cog.EasyCog",
"social_score.cog.SocialScoreCog",
"gam_coins.cog.GamCoinsCog",
"discord_bot.cog.UserTrackingCog",
Expand Down

0 comments on commit 9117539

Please sign in to comment.