-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
92 lines (71 loc) · 2.83 KB
/
main.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
86
87
88
89
90
91
92
from __future__ import annotations
import os
from typing import TYPE_CHECKING, List
import logzero
import toml
from twitchio.ext import commands
from utils import Config
if TYPE_CHECKING:
from twitchio import Channel, Message
logger: logzero.logging = logzero.logger
__ver__ = "1.0.1"
class Bot(commands.Bot):
def __init__(self, config: Config) -> None:
self.config = config
# case_insensitive=True にしておくとコマンドの大文字小文字が関係なくなる
super().__init__(**self.config.irc_login, case_insensitive=True)
# ./cogs内の.pyファイルをcogとして登録する
if not self.cogs:
cogs = self.get_cogs()
for cog in cogs:
self.load_module(cog)
logger.info(f"{cog.removeprefix('cogs.')} を読み込みました")
def get_cogs(self) -> List[str]:
"""
./cogs内の.pyファイルをリストで返す
"""
cog_files = os.listdir("./cogs")
return [
f'cogs.{file.removesuffix(".py")}'
for file in cog_files
if file.endswith(".py")
]
async def event_channel_joined(self, channel: Channel) -> None:
"""
ただの準備完了のお知らせ
"""
logger.info(f"{channel.name}に接続しました")
async def event_message(self, message: Message) -> None:
"""
メッセージのログ用
"""
if message.echo:
logger.info(f"[{message.channel.name}]{self.nick}: {message.content}")
return
logger.info(
f"[{message.channel.name}]{message.author.display_name}: {message.content}"
)
# 親クラスでevent_messageからコマンドを呼び出しているので、
# 子クラスでオーバーライドするときはこれやらないとコマンドが呼び出されない。
# 子クラスでmessage.echoをreturnしてるなら
# await self.handle_commands(message)
# でもいいと思います。
await super().event_message(message)
async def event_command_error(
self, context: commands.Context, error: Exception
) -> None:
"""
コマンドが見つからなかったりエラーが起きると呼び出されるメソッド。
親クラスのprint文が気になるので。お好みで。
"""
logger.error(f"コマンドエラー: {error}")
@commands.command()
async def ver(self, ctx: commands.Context) -> None:
await ctx.reply(f"バージョン: {__ver__}")
if __name__ == "__main__":
config = Config(**toml.load("config.toml"))
# loggerの設定
# 個人的な好みでlogzeroを使用します
logzero.setup_logger(isRootLogger=True, level=config.loglevel)
bot = Bot(config)
bot.run()