33
44import asyncio
55import concurrent .futures
6+ import logging
67import re
78import sqlite3
89import time
1213import discord
1314from discord import Embed , Message , app_commands
1415from discord .ext import commands
16+ from utils .config import guild_config
1517from utils .format import (
16- build_error_embed , build_loot_entries , build_point_entries ,
17- build_success_embed , make_time , normalize_user ,
18+ build_epgp_footer , build_error_embed , build_loot_entries ,
19+ build_point_entries , build_success_embed , normalize_user ,
1820)
1921from utils .response import Response , ResponseStatus
2022from utils .savedvariables_parser import SavedVariablesParser
@@ -29,19 +31,25 @@ class Epgp(commands.Cog):
2931 def __init__ (self , bot : SlaoBot ):
3032 """Cog to handle EPGP."""
3133 self .bot : SlaoBot = bot
32- self ._check_db ()
3334
3435 @commands .Cog .listener ()
3536 async def on_message (self , message : Message ) -> None :
3637 # Do not listen DM channels
3738 if isinstance (message .channel , discord .DMChannel ):
3839 return
3940
41+ guild_id = str (message .guild .id )
42+ if not self ._check_settings (guild_id ):
43+ return
44+
45+ if message .channel .id != guild_config [guild_id ].getint ('epgp_upload_channel' ):
46+ return
47+
4048 # Process only message with attachments
4149 if len (message .attachments ) == 0 :
4250 return
4351
44- response = await self ._process_attachment (message )
52+ response = await self ._process_attachment (message , guild_id )
4553
4654 # Provide response
4755 await self .send_response (message .channel , response .data )
@@ -53,10 +61,14 @@ async def epgp(self, interaction: discord.Interaction, target: Optional[str] = N
5361 # noinspection PyUnresolvedReferences
5462 await interaction .response .defer ()
5563
64+ guild_id = str (interaction .guild .id )
65+ if not self ._check_settings (guild_id ):
66+ return
67+
5668 if target is None :
5769 target = normalize_user (interaction .user )
5870
59- embed = await self .process_epgp (target )
71+ embed = await self .process_epgp (target , guild_id )
6072 if embed is None :
6173 embed = Embed .from_dict (build_error_embed ('Нет информации по персонажу {0}' .format (target )))
6274
@@ -68,7 +80,11 @@ async def raidloot(self, interaction: discord.Interaction) -> None:
6880 # noinspection PyUnresolvedReferences
6981 await interaction .response .defer ()
7082
71- embed = await self .process_raidloot ()
83+ guild_id = str (interaction .guild .id )
84+ if not self ._check_settings (guild_id ):
85+ return
86+
87+ embed = await self .process_raidloot (guild_id )
7288 if embed is None :
7389 embed = Embed .from_dict (build_error_embed ('Нет информации по луту' ))
7490
@@ -80,14 +96,19 @@ async def points(self, interaction: discord.Interaction) -> None:
8096 # noinspection PyUnresolvedReferences
8197 await interaction .response .defer ()
8298
83- embed = await self .process_points ()
99+ guild_id = str (interaction .guild .id )
100+ if not self ._check_settings (guild_id ):
101+ return
102+
103+ embed = await self .process_points (guild_id )
84104 if embed is None :
85105 embed = Embed .from_dict (build_error_embed ('Нет информации по начислениям' ))
86106
87107 await interaction .edit_original_response (embed = embed )
88108
89- async def process_epgp (self , target : str ) -> Optional [discord .Embed ]:
90- connection : Connection = sqlite3 .connect ('./data/epgp.db' )
109+ async def process_epgp (self , target : str , guild_id : str ) -> Optional [discord .Embed ]:
110+ db_name = f'./data/{ guild_id } .db'
111+ connection : Connection = sqlite3 .connect (db_name )
91112 cursor : Cursor = connection .cursor ()
92113
93114 cursor .execute ('''SELECT * FROM Standing WHERE player=?''' , (target ,))
@@ -120,17 +141,15 @@ async def process_epgp(self, target: str) -> Optional[discord.Embed]:
120141 inline = False )
121142
122143 # Footer
123- cursor .execute ('''SELECT * FROM History ORDER BY TIMESTAMP DESC''' )
124- _ , user , timestamp = cursor .fetchone ()
125- embed .set_footer (text = 'Последнее обновление - {0} | {1}' .
126- format (user , '{0:16}' .format (make_time (timestamp ))))
144+ embed .set_footer (text = build_epgp_footer (guild_id ))
127145
128146 cursor .close ()
129147 connection .close ()
130148 return embed
131149
132- async def process_raidloot (self ) -> Optional [discord .Embed ]:
133- connection : Connection = sqlite3 .connect ('./data/epgp.db' )
150+ async def process_raidloot (self , guild_id : str ) -> Optional [discord .Embed ]:
151+ db_name = f'./data/{ guild_id } .db'
152+ connection : Connection = sqlite3 .connect (db_name )
134153 cursor : Cursor = connection .cursor ()
135154
136155 embed = Embed (title = 'Последние 30 предметов' , description = '' , colour = 10204605 )
@@ -144,17 +163,15 @@ async def process_raidloot(self) -> Optional[discord.Embed]:
144163 inline = False )
145164
146165 # Footer
147- cursor .execute ('''SELECT * FROM History ORDER BY TIMESTAMP DESC''' )
148- _ , user , timestamp = cursor .fetchone ()
149- embed .set_footer (text = 'Последнее обновление - {0} | {1}' .
150- format (user , '{0:16}' .format (make_time (timestamp ))))
166+ embed .set_footer (text = build_epgp_footer (guild_id ))
151167
152168 cursor .close ()
153169 connection .close ()
154170 return embed
155171
156- async def process_points (self ) -> Optional [discord .Embed ]:
157- connection : Connection = sqlite3 .connect ('./data/epgp.db' )
172+ async def process_points (self , guild_id : str ) -> Optional [discord .Embed ]:
173+ db_name = f'./data/{ guild_id } .db'
174+ connection : Connection = sqlite3 .connect (db_name )
158175 cursor : Cursor = connection .cursor ()
159176
160177 embed = Embed (title = 'Последние начисления' , description = '' , colour = 10204605 )
@@ -168,10 +185,7 @@ async def process_points(self) -> Optional[discord.Embed]:
168185 inline = False )
169186
170187 # Footer
171- cursor .execute ('''SELECT * FROM History ORDER BY TIMESTAMP DESC''' )
172- _ , user , timestamp = cursor .fetchone ()
173- embed .set_footer (text = 'Последнее обновление - {0} | {1}' .
174- format (user , '{0:16}' .format (make_time (timestamp ))))
188+ embed .set_footer (text = build_epgp_footer (guild_id ))
175189
176190 cursor .close ()
177191 connection .close ()
@@ -194,10 +208,11 @@ async def send_response(self, channel, responses) -> None:
194208 # Respond with embed
195209 await channel .send (embed = Embed .from_dict (response ))
196210
197- def _check_db (self ):
198- self .connection : Connection = sqlite3 .connect ('./data/epgp.db' )
199- self .cursor : Cursor = self .connection .cursor ()
200- self .cursor .execute ('''
211+ def _check_db (self , guild_id : str ) -> None :
212+ db_name = f'./data/{ guild_id } .db'
213+ connection : Connection = sqlite3 .connect (db_name )
214+ cursor : Cursor = connection .cursor ()
215+ cursor .execute ('''
201216 CREATE TABLE IF NOT EXISTS Traffic (
202217 id INTEGER PRIMARY KEY,
203218 target TEXT,
@@ -214,26 +229,41 @@ def _check_db(self):
214229 unit_guid TEXT
215230 )
216231 ''' )
217- self . cursor .execute ('''
232+ cursor .execute ('''
218233 CREATE TABLE IF NOT EXISTS History (
219234 id INTEGER PRIMARY KEY,
220235 user TEXT NOT NULL,
221236 timestamp INTEGER NOT NULL
222237 )
223238 ''' )
224- self . cursor .execute ('''
239+ cursor .execute ('''
225240 CREATE TABLE IF NOT EXISTS Standing (
226241 id INTEGER PRIMARY KEY,
227242 player TEXT NOT NULL,
228243 ep INTEGER NOT NULL,
229244 gp INTEGER NOT NULL
230245 )
231246 ''' )
232- self .connection .commit ()
233- self .cursor .close ()
234- self .connection .close ()
247+ connection .commit ()
248+ cursor .close ()
249+ connection .close ()
250+
251+ def _check_settings (self , guild_id : str ) -> bool :
252+ if not guild_config .has_section (guild_id ):
253+ logging .error (f'Guild config not found for guild id { guild_id } ' )
254+ return False
255+
256+ if not guild_config [guild_id ].getboolean ('epgp_enabled' ):
257+ return False
258+
259+ if not guild_config [guild_id ].getint ('epgp_upload_channel' ):
260+ return False
261+
262+ self ._check_db (guild_id )
263+
264+ return True
235265
236- async def _process_attachment (self , message : Message ) -> Response :
266+ async def _process_attachment (self , message : Message , guild_id : str ) -> Response :
237267 for attachment in message .attachments :
238268
239269 # Process only specific file name
@@ -255,11 +285,13 @@ async def _process_attachment(self, message: Message) -> Response:
255285 pool ,
256286 self ._process_sv ,
257287 attachment_bytes .decode ('utf-8' , errors = 'replace' ),
258- message .author )
288+ message .author ,
289+ guild_id ,
290+ )
259291
260292 return response
261293
262- def _process_sv (self , data : str , author ) -> Response :
294+ def _process_sv (self , data : str , author , guild_id : str ) -> Response :
263295
264296 addon_data = self ._check_upload (data )
265297 if addon_data is None :
@@ -283,7 +315,8 @@ def _process_sv(self, data: str, author) -> Response:
283315 ('Standing data not found in .lua file. '
284316 'Check if you have provided proper savedvariable file.' ))
285317
286- self .connection : Connection = sqlite3 .connect ('./data/epgp.db' )
318+ db_name = f'./data/{ guild_id } .db'
319+ self .connection : Connection = sqlite3 .connect (db_name )
287320 self .cursor : Cursor = self .connection .cursor ()
288321
289322 # Process Traffic
0 commit comments