This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
85 lines (72 loc) · 3.6 KB
/
bot.py
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
import hikari
import lightbulb
import miru
import aiohttp
import os
import sys
from dotenv import load_dotenv
import logging
import dataset
import functions.authManager as authManager
# Load .env values
load_dotenv()
# Create logger
logger = logging.getLogger('wacka.bot')
# Initialize bot instance
# default_enabled_guilds=(884484987758473217,623015907995811840,839466562460188682)
bot = lightbulb.BotApp(token=os.getenv('TOKEN'), prefix=os.getenv('PREFIX'), banner=None, help_slash_command=True, intents=hikari.Intents.ALL_UNPRIVILEGED)
miru.load(bot)
@bot.listen()
async def on_starting(event: hikari.StartingEvent) -> None:
# Create a ClientSession to be used for the entire uptime of the bot
bot.d.aio_session = aiohttp.ClientSession()
logger.info("Created aiohttp.ClientSession")
bot.d.logger = logger
logger.info("Added logger to datastore")
bot.d.db = dataset.connect(os.getenv('DATABASE'), engine_kwargs=dict(connect_args={'check_same_thread': False}))
logger.info(f"Connected to database {os.getenv('DATABASE')}")
# Login to Aime and get a fresh new session cookie
loginStatus = await authManager.loginWithAimeID(bot)
if loginStatus == True:
logger.info("Logged in with Aime")
else:
logger.error("Failed to login with Aime, likely due to maintenance")
@bot.listen()
async def on_stopping(event: hikari.StoppingEvent) -> None:
# Log out of Aime
logoutStatus = await authManager.logout(bot)
if logoutStatus == True:
logger.info("Logged out of Aime")
else:
logger.error("Failed to logout of Aime, likely due to maintenance")
bot.d.db.close()
logger.info("Closed database connection")
# Close the ClientSession
await bot.d.aio_session.close()
logger.info("Closed aiohttp.ClientSession")
# Update presence on start
@bot.listen()
async def on_started(event: hikari.StartedEvent) -> None:
await bot.update_presence(activity=hikari.Activity(name="/help | WACCA! 🧤"))
logger.info("Updated presence")
# Error handling
@bot.listen(lightbulb.CommandErrorEvent)
async def on_error(event: lightbulb.CommandErrorEvent) -> None:
if isinstance(event.exception, lightbulb.CommandInvocationError):
await event.context.respond(f"<:no:442206260151189518> An error occurred while running `{event.context.command.name}`. Try again in a minute. If this persists, please contact Burrito at <https://website.burrito.software/discord>.\n`{event.context.command.name}`の実行中にエラーが発生しました。これでも解決しない場合は、ブリトー(<https://website.burrito.software/discord>)までご連絡ください。", flags=hikari.MessageFlag.EPHEMERAL)
raise event.exception
# Unwrap the exception to get the original cause
exception = event.exception.__cause__ or event.exception
if isinstance(exception, lightbulb.NotOwner):
await event.context.respond("<:no:442206260151189518> You are not the owner of this bot.\nあなたはこのボットの所有者ではありません。", flags=hikari.MessageFlag.EPHEMERAL)
elif isinstance(exception, lightbulb.CommandIsOnCooldown):
await event.context.respond(f":hourglass: This command is on cooldown. Retry in `{exception.retry_after:.2f}` seconds.\nこのコマンドはクールダウン中です。リトライは `{exception.retry_after:.2f}` 秒後に行われます。", flags=hikari.MessageFlag.EPHEMERAL)
else:
raise exception
# Loading all extensions
bot.load_extensions_from("./extensions/", must_exist=True)
# uvloop for performance on UNIX systems
if os.name != "nt":
import uvloop
uvloop.install()
bot.run()