Skip to content

Commit

Permalink
Upgrade to Discord.py 2.0 (#736)
Browse files Browse the repository at this point in the history
* Upgrade to Discord.py 2.0

This enables the new privileged intent and gets us running again. We're now hardcoding the prefix in the help text, but that's because of a limitation in the upstream library.

* Fix Poetry installer

* Update path to Poetry

* Get CI passing
  • Loading branch information
thebeanogamer authored Oct 22, 2022
1 parent f462f22 commit f1ac1b0
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 181 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ jobs:
${{ runner.os }}-
- name: Install Poetry
run: curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python
run: curl -sSL https://install.python-poetry.org | python3 -

- name: Disable Virtualenvs
run: $HOME/.poetry/bin/poetry config virtualenvs.create false
run: $HOME/.local/bin/poetry config virtualenvs.create false

- name: Install Python Dependencies
run: $HOME/.poetry/bin/poetry install --no-interaction --no-ansi
run: $HOME/.local/bin/poetry install --no-interaction --no-ansi
env:
PIP_CACHE_DIR: ~/.pip

Expand Down
7 changes: 4 additions & 3 deletions cdbot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
from git import Repo
from sentry_sdk.integrations.aiohttp import AioHttpIntegration

from .bot import bot
from .bot import bot, load_extensions
from .constants import BOT_TOKEN, SENTRY_URL


def main():
async def main():
"""Entry point for poetry script."""
sentry_sdk.init(
SENTRY_URL,
release=Repo().head.object.hexsha,
integrations=[AioHttpIntegration()],
)
bot.run(BOT_TOKEN)
await load_extensions()
await bot.start(BOT_TOKEN)
4 changes: 3 additions & 1 deletion cdbot/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Entry point for the 'python -m cdbot' command."""

from asyncio import run

import cdbot

if __name__ == "__main__":
cdbot.main()
run(cdbot.main())
14 changes: 7 additions & 7 deletions cdbot/bot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Main script to define bot methods, and start the bot."""

import logging
from os import listdir
from platform import release, system

from discord import Game, Intents
Expand All @@ -13,6 +14,7 @@

intents = Intents.default()
intents.members = True
intents.message_content = True

bot = Bot(command_prefix=when_mentioned_or("...", ":"), activity=Game(name=":help"), intents=intents)

Expand Down Expand Up @@ -46,10 +48,8 @@ async def block_muted(ctx):
return ctx.author.id not in bot.muted


# Load cogs
bot.load_extension("cdbot.cogs.general")
bot.load_extension("cdbot.cogs.cyber")
bot.load_extension("cdbot.cogs.fun")
bot.load_extension("cdbot.cogs.admin")
bot.load_extension("cdbot.cogs.maths")
bot.load_extension("cdbot.cogs.help")
async def load_extensions():
for filename in listdir("./cdbot/cogs"):
if filename.endswith(".py"):
# cut off the .py from the file name
await bot.load_extension(f"cdbot.cogs.{filename[:-3]}")
4 changes: 2 additions & 2 deletions cdbot/cogs/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,5 @@ async def raid(
await logs_channel.send(embed=embed) # log the event


def setup(bot):
bot.add_cog(Admin(bot))
async def setup(bot):
await bot.add_cog(Admin(bot))
4 changes: 2 additions & 2 deletions cdbot/cogs/cyber.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,5 +531,5 @@ async def on_message(self, message: Message):
break


def setup(bot):
bot.add_cog(Cyber(bot))
async def setup(bot):
await bot.add_cog(Cyber(bot))
4 changes: 2 additions & 2 deletions cdbot/cogs/fun.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,8 +744,8 @@ async def murder(self, ctx: Context):
await ctx.send("rm -rf / --no-preserve-root")


def setup(bot):
async def setup(bot):
"""
Required boilerplate for adding functionality of cog to bot.
"""
bot.add_cog(Fun(bot))
await bot.add_cog(Fun(bot))
4 changes: 2 additions & 2 deletions cdbot/cogs/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,5 @@ async def tos(self, ctx: Context):
await ctx.send("https://www.discord.com/terms")


def setup(bot):
bot.add_cog(General(bot))
async def setup(bot):
await bot.add_cog(General(bot))
20 changes: 10 additions & 10 deletions cdbot/cogs/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ async def send_bot_help(self, mapping):
ctx = self.context
embed = Embed(
color=Colour.blue(),
description=f"Type ``{self.clean_prefix}help [command]`` for more info on a command.\n"
description="Type ``:help [command]`` for more info on a command.\n"
"You can also type "
f"``{self.clean_prefix}help [category]`` for more info on a category."
"``:help [category]`` for more info on a category."
)
for cog in mapping.keys():
if cog is not None:
if cog.get_commands():
embed.add_field(name=cog.qualified_name,
value=f"``{self.clean_prefix}help {cog.qualified_name.lower()}``")
value=f"``:help {cog.qualified_name.lower()}``")

dm = await ctx.author.send(embed=embed)
if ctx.guild is not None:
Expand All @@ -41,12 +41,12 @@ async def send_cog_help(self, cog):
embed = Embed(
color=Colour.blue(),
title=cog.description,
description=f"Type ``{self.clean_prefix}help [command]`` for more info on a command.\n You can also type "
f"``{self.clean_prefix}help [category]`` for more info on a different category."
description="Type ``:help [command]`` for more info on a command.\n You can also type "
"``:help [category]`` for more info on a different category."
)
for command in cog.get_commands():
if command.hidden is not True:
embed.add_field(name=f"``{self.clean_prefix}{command.name}``", value=command.help)
embed.add_field(name=f"``:{command.name}``", value=command.help)

dm = await ctx.author.send(embed=embed)
if ctx.guild is not None:
Expand All @@ -61,11 +61,11 @@ async def send_command_help(self, command):
if command.hidden is not True:
embed = Embed(
color=Colour.blue(),
title=f"``{self.clean_prefix}{command.name}``",
title=f"``:{command.name}``",
description=command.help
)
embed.add_field(name="Usage",
value=f"``{self.clean_prefix}{command.name} {command.signature}``",
value=f"``:{command.name} {command.signature}``",
inline=False)
if command.aliases:
embed.add_field(name="Aliases",
Expand All @@ -86,5 +86,5 @@ def cog_unload(self):
self.bot.help_command = self.bot._original_help_command


def setup(bot):
bot.add_cog(Help(bot))
async def setup(bot):
await bot.add_cog(Help(bot))
Loading

0 comments on commit f1ac1b0

Please sign in to comment.