forked from Zentro/RoRServerBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrorbot.py
More file actions
179 lines (158 loc) · 6.13 KB
/
rorbot.py
File metadata and controls
179 lines (158 loc) · 6.13 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import asyncio
import aiosqlite
import argparse
from argparse import RawTextHelpFormatter
from typing import List
import logging
import logging.handlers
import discord
from discord.ext import commands
from aiohttp import ClientSession
from config import Config
# -----------------------------------------------------------------------------
# Constants
# -----------------------------------------------------------------------------
__version__ = "2.0.0"
class Main(commands.Bot):
def __init__(
self,
*args,
initial_extensions: List[str],
logger: logging.Logger,
web_client: ClientSession,
**kwargs):
super().__init__(*args, **kwargs)
self.initial_extensions = initial_extensions
self.logger = logger
self.web_client = web_client
self.db = None
async def setup_hook(self) -> None:
# Load the extenions prior to sync to ensure we are syncing
# interactions defined in the extensions...
for extension in self.initial_extensions:
try:
await self.load_extension(extension)
except Exception as e:
self.logger.error(f"Error loading extension {extension}: {e}")
# Connect to the sqlite database and establish our connection pool
# including loading anything into memory prior to handling events...
await self.connect_db()
async def connect_db(self):
"""
Establish a connection to the sqlite database.
Raises:
ValueError: If the database connection could not be established.
"""
try:
self.db = await aiosqlite.connect('rorserverbot.db')
self.logger.info("Database connection established.")
await self.setup_db()
except Exception as e:
self.logger.error(
f"Error trying to establish connection to database: {e}")
async def setup_db(self):
"""
Setup the database tables if they don't already exist.
Raises:
Exception: If there is an issue writing to the database, such as a
connection issue.
"""
async with self.db.cursor() as cursor:
await cursor.execute('''
CREATE TABLE IF NOT EXISTS servers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
ip TEXT UNIQUE,
password TEXT NULL,
channel INTEGER UNIQUE,
active INTEGER NOT NULL CHECK (active IN (0, 1))
);
''')
await self.db.commit()
self.logger.info("Database setup completed.")
async def close_db(self):
"""
Close the database connection pool.
Raises:
Exception: If there is an issue of safely closing the coonnection
pool.
"""
if self.db:
try:
await self.db.close()
self.logger.info("Database connection closed.")
except Exception as e:
self.logger.error(
f"Error closing the database connection: {e}")
finally:
self.db = None
def print_version_and_quit():
"""
"""
async def main():
# ------------------------------------------------------------------------
# Setup the command line arguments
# ------------------------------------------------------------------------
parser = argparse.ArgumentParser(
description="",
formatter_class=RawTextHelpFormatter)
parser.add_argument("--config",
required=True,
help="Path to the configuration file")
parser.add_argument("--version",
help="Print the version and quit")
args = parser.parse_args()
# ------------------------------------------------------------------------
# Load the configuration file
# ------------------------------------------------------------------------
Config.load_config(args.config)
config = Config.get_config()
# ------------------------------------------------------------------------
# Setup the logger
# ------------------------------------------------------------------------
# logger = logging.getLogger(config["application"]["name"])
logger = logging.getLogger('RoRBot')
logger.setLevel(Config.get_logging_level())
handler = logging.handlers.RotatingFileHandler(
filename="rorserverbot.log",
encoding="utf-8",
maxBytes=32 * 1024 * 1024,
backupCount=5,
)
dt_fmt = '%Y-%m-%d %H:%M:%S'
formatter = logging.Formatter(
'[{asctime}] [{threadName:21s}] [{levelname:<8}] {name}: {message}',
dt_fmt, style='{')
handler.setFormatter(formatter)
logger.addHandler(handler)
# ------------------------------------------------------------------------
# Setup the Discord bot
# ------------------------------------------------------------------------
exts = ['extensions.servers']
intents = discord.Intents.default()
intents.message_content = True
async with ClientSession() as our_client:
async with Main(
commands.when_mentioned,
initial_extensions=exts,
logger=logger,
web_client=our_client,
intents=intents,
) as our_bot:
await our_bot.start(config['discord']['token'])
asyncio.run(main())