aboutsummaryrefslogtreecommitdiffstats
path: root/dev/bot
diff options
context:
space:
mode:
authorGravatar Hassan Abouelela <[email protected]>2022-07-23 17:17:29 +0200
committerGravatar GitHub <[email protected]>2022-07-23 17:17:29 +0200
commit94c4b408f1afa604ae6907aa28ab694870af20f2 (patch)
tree6a93d9648d033a4e535424defe3eaa407eb41dc5 /dev/bot
parentMerge pull request #99 from python-discord/dependabot/pip/pre-commit-2.20.0 (diff)
parentDocument Sample Project Environment Variables (diff)
Merge pull request #107 from python-discord/better-development
Fix Docker Compose & Add Boilerplate Project
Diffstat (limited to 'dev/bot')
-rw-r--r--dev/bot/__init__.py24
-rw-r--r--dev/bot/__main__.py34
-rw-r--r--dev/bot/cog.py33
3 files changed, 91 insertions, 0 deletions
diff --git a/dev/bot/__init__.py b/dev/bot/__init__.py
new file mode 100644
index 00000000..71871209
--- /dev/null
+++ b/dev/bot/__init__.py
@@ -0,0 +1,24 @@
+import asyncio
+import logging
+import os
+import sys
+
+import botcore
+
+if os.name == "nt":
+ # Change the event loop policy on Windows to avoid exceptions on exit
+ asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
+
+# Some basic logging to get existing loggers to show
+logging.getLogger().addHandler(logging.StreamHandler())
+logging.getLogger().setLevel(logging.DEBUG)
+logging.getLogger("discord").setLevel(logging.ERROR)
+
+
+class Bot(botcore.BotBase):
+ """Sample Bot implementation."""
+
+ async def setup_hook(self) -> None:
+ """Load extensions on startup."""
+ await super().setup_hook()
+ asyncio.create_task(self.load_extensions(sys.modules[__name__]))
diff --git a/dev/bot/__main__.py b/dev/bot/__main__.py
new file mode 100644
index 00000000..00ebdefc
--- /dev/null
+++ b/dev/bot/__main__.py
@@ -0,0 +1,34 @@
+import asyncio
+import os
+
+import aiohttp
+import discord
+import dotenv
+from discord.ext import commands
+
+import botcore
+from . import Bot
+
+dotenv.load_dotenv()
+botcore.utils.apply_monkey_patches()
+
+roles = os.getenv("ALLOWED_ROLES")
+roles = [int(role) for role in roles.split(",")] if roles else []
+
+bot = Bot(
+ guild_id=int(os.getenv("GUILD_ID")),
+ http_session=None, # type: ignore # We need to instantiate the session in an async context
+ allowed_roles=roles,
+ command_prefix=commands.when_mentioned_or(os.getenv("PREFIX", "!")),
+ intents=discord.Intents.all(),
+ description="Bot-core test bot.",
+)
+
+
+async def main() -> None:
+ """Run the bot."""
+ bot.http_session = aiohttp.ClientSession()
+ async with bot:
+ await bot.start(os.getenv("TOKEN"))
+
+asyncio.run(main())
diff --git a/dev/bot/cog.py b/dev/bot/cog.py
new file mode 100644
index 00000000..7746c54e
--- /dev/null
+++ b/dev/bot/cog.py
@@ -0,0 +1,33 @@
+from discord.ext import commands
+
+from . import Bot
+
+
+class Cog(commands.Cog):
+ """A simple discord.py cog."""
+
+ def __init__(self, _bot: Bot):
+ self.bot = _bot
+
+ @commands.Cog.listener()
+ async def on_ready(self) -> None:
+ """Print a message when the client (re)connects."""
+ print("Client is ready.")
+
+ @commands.command()
+ async def reload(self, ctx: commands.Context) -> None:
+ """Reload all available cogs."""
+ message = await ctx.send(":hourglass_flowing_sand: Reloading")
+ for ext in list(self.bot.extensions):
+ await self.bot.reload_extension(ext)
+ await message.edit(content=":white_check_mark: Done")
+
+ @commands.command()
+ async def ping(self, ctx: commands.Context) -> None:
+ """Test if the bot is online."""
+ await ctx.send("We are live!")
+
+
+async def setup(_bot: Bot) -> None:
+ """Install the cog."""
+ await _bot.add_cog(Cog(_bot))