-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbot.py
More file actions
113 lines (85 loc) · 2.84 KB
/
bot.py
File metadata and controls
113 lines (85 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import asyncio
import logging
import os
import random
import tomllib
import discord
from discord.ext import commands, tasks
from pretty_help import PrettyHelp
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker
log = logging.getLogger(__name__)
async def main():
# start the client
async with client:
for filename in os.listdir("./cogs"):
if filename.endswith(".py"):
await client.load_extension(f"cogs.{filename[:-3]}")
await client.start(client.config["general"]["token"])
engine = create_async_engine("sqlite+aiosqlite:///data/database.sqlite", echo=None)
Session = sessionmaker(engine, expire_on_commit=False, class_=AsyncSession)
class Bot(commands.Bot):
def __init__(self, *args, **kwargs):
with open("config.toml", "rb") as f:
self.config = tomllib.load(f)
kwargs["command_prefix"] = self.config["general"]["prefix"]
super().__init__(*args, **kwargs)
@property
def session(self):
return Session()
intents = discord.Intents().all()
client = Bot(
intents=intents,
help_command=PrettyHelp(),
)
@tasks.loop(seconds=30)
async def change_status():
statuses = [
",help | ,invite",
f"Currently in {len(client.guilds)} guilds",
f"I can see {len(client.users)} users",
]
await client.change_presence(activity=discord.Game(random.choice(statuses)))
@client.command(hidden=True)
async def load(ctx, extension):
if ctx.author.id == client.config["general"]["owner_id"]:
await client.load_extension(f"cogs.{extension}")
await ctx.send(f"{extension} loaded.")
else:
await ctx.send("no")
@client.command(hidden=True)
async def unload(ctx, extension):
if ctx.author.id == client.config["general"]["owner_id"]:
await client.unload_extension(f"cogs.{extension}")
await ctx.send(f"{extension} unloaded.")
else:
await ctx.send("no")
@client.command(hidden=True)
async def reload(ctx, extension):
if ctx.author.id == client.config["general"]["owner_id"]:
await client.unload_extension(f"cogs.{extension}")
await ctx.send(f"{extension} unloaded.")
await client.load_extension(f"cogs.{extension}")
await ctx.send(f"{extension} loaded.")
else:
await ctx.send("no")
async def guilds():
return client.guilds
async def users():
return client.users
@client.event
async def on_ready():
log.info("I am ready.")
change_status.start()
try:
synced = await client.tree.sync()
log.info(f"Loaded {len(synced)} commands!")
except Exception as e:
print(e)
if __name__ == "__main__":
try:
discord.utils.setup_logging()
asyncio.run(main())
except KeyboardInterrupt:
# Handle gracefully
log.info("Stopping")