Skip to content

Commit

Permalink
python 3.11, discord.py 2.1 -> async setup, pathlib instead of os.pat…
Browse files Browse the repository at this point in the history
…h, environment variable imstead of .env file

singleton Database, Bot subclass, optimization, reduction, docstrings
easter egg :)
  • Loading branch information
KruASe76 committed Dec 19, 2022
1 parent 1a09a5c commit d864fc6
Show file tree
Hide file tree
Showing 14 changed files with 491 additions and 661 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
.vscode/
.idea/
.fleet/

/env*/
/venv*/

__pycache__/
*.db-journal
.DS_Store

/.env

/base.db
/images/

Expand Down
Binary file modified README.md
Binary file not shown.
85 changes: 47 additions & 38 deletions bot.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,61 @@
import os
from typing import Iterable

from discord import Intents, Message
from discord.ext.commands import Bot, when_mentioned_or
import asyncio
from dotenv import find_dotenv, get_key
from typing import Iterable

from misc.database import Database
from handlers.commands import add_commands
from handlers.events import add_events
from handlers.help import add_help
from handlers.commands import add_commands
from misc.constants import ADMINS
from misc.database import Database


class CodenamesBot(Bot):
db: Database = None

@classmethod
def custom_intents(cls) -> Intents:
intents = Intents.default()
intents.message_content = True
return intents

async def setup_hook(self) -> None:
await Database.create()
self.db = Database()

def main() -> None:
loop = asyncio.get_event_loop()
db = loop.run_until_complete(Database.create())

async def get_prefix(bot: Bot, message: Message) -> Iterable[str]:
if message.guild:
prefix = (await db.fetch("SELECT prefix FROM guilds WHERE id=?", (message.guild.id,)))[0]
else:
request = (await db.fetch("SELECT prefix FROM players WHERE id=?", (message.author.id,)))

if not request:
await db.exec_and_commit(
"INSERT INTO players VALUES (?,strftime('%d/%m/%Y','now'),?,?,?,?,?,?)",
(message.author.id, "", "en", 0, 0, 0, 0)
)

prefix = request[0] if request else ""

res = (prefix, "cdn") if prefix else ("cdn",)

return when_mentioned_or(*res)(bot, message)

intents: Intents = Intents.default()
# intents.message_content = True
bot = Bot(
await add_events(self)
await add_commands(self)
await add_help(self)


async def get_prefix(bot: CodenamesBot, message: Message) -> Iterable[str]:
if message.guild:
prefix = (await bot.db.fetch("SELECT prefix FROM guilds WHERE id=?", (message.guild.id,)))[0]
else:
request = (await bot.db.fetch("SELECT prefix FROM players WHERE id=?", (message.author.id,)))

if not request:
await bot.db.exec_and_commit(
"INSERT INTO players VALUES (?,strftime('%d/%m/%Y','now'),?,?,?,?,?,?)",
(message.author.id, "", "en", 0, 0, 0, 0)
)

prefix = request[0] if request else ""

res = (prefix, "cdn") if prefix else ("cdn",)

return when_mentioned_or(*res)(bot, message)


async def main() -> None:
bot = CodenamesBot(
command_prefix=get_prefix,
help_command=None,
strip_after_prefix=True,
intents=intents,
intents=CodenamesBot.custom_intents(),
owner_ids=ADMINS
)

add_events(bot, db)
add_commands(bot, db)
add_help(bot, db)


TOKEN = get_key(find_dotenv(), "TOKEN")
bot.run(TOKEN)
token = os.environ.get("TOKEN")
await bot.start(token)
8 changes: 5 additions & 3 deletions handlers/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@


def is_moderator():
def predicate(ctx: Context):
if not (ctx.message.author.permissions_in(ctx.channel).manage_messages or ctx.bot.is_owner(ctx.message.author)):
raise MissingPermissions(None)
def predicate(ctx: Context) -> bool:
if not (
ctx.channel.permissions_for(ctx.message.author).manage_messages or ctx.bot.is_owner(ctx.message.author)
):
raise MissingPermissions(["manage_messages"])

return True

Expand Down
Loading

0 comments on commit d864fc6

Please # to comment.