31
31
import sys
32
32
33
33
from datetime import datetime
34
+ from pkg_resources import parse_version
34
35
from types import SimpleNamespace
35
36
36
37
import discord
45
46
from motor .motor_asyncio import AsyncIOMotorClient
46
47
47
48
from core .changelog import Changelog
48
- from core .clients import ModmailApiClient , SelfHostedClient , PluginDatabaseClient
49
+ from core .clients import SelfHostedClient , PluginDatabaseClient
49
50
from core .config import ConfigManager
50
51
from core .utils import info , error
51
52
from core .models import Bot
@@ -104,14 +105,10 @@ def __init__(self):
104
105
105
106
self ._configure_logging ()
106
107
107
- if self .self_hosted :
108
- self ._db = AsyncIOMotorClient (self .config .mongo_uri ).modmail_bot
109
- self ._api = SelfHostedClient (self )
110
- else :
111
- self ._api = ModmailApiClient (self )
108
+ self ._db = AsyncIOMotorClient (self .config .mongo_uri ).modmail_bot
109
+ self ._api = SelfHostedClient (self )
112
110
self .plugin_db = PluginDatabaseClient (self )
113
111
114
- self .data_task = self .loop .create_task (self .data_loop ())
115
112
self .autoupdate_task = self .loop .create_task (self .autoupdate_loop ())
116
113
self ._load_extensions ()
117
114
self .owner = None
@@ -144,10 +141,6 @@ def version(self):
144
141
def db (self ):
145
142
return self ._db
146
143
147
- @property
148
- def self_hosted (self ):
149
- return bool (self .config .get ('mongo_uri' , '' ))
150
-
151
144
@property
152
145
def api (self ):
153
146
return self ._api
@@ -210,12 +203,6 @@ def run(self, *args, **kwargs):
210
203
except Exception :
211
204
logger .critical (error ('Fatal exception' ), exc_info = True )
212
205
finally :
213
- try :
214
- self .data_task .cancel ()
215
- self .loop .run_until_complete (self .data_task )
216
- except asyncio .CancelledError :
217
- logger .debug ('data_task has been cancelled' )
218
-
219
206
try :
220
207
self .autoupdate_task .cancel ()
221
208
self .loop .run_until_complete (self .autoupdate_task )
@@ -345,15 +332,8 @@ def main_color(self):
345
332
346
333
async def on_connect (self ):
347
334
logger .info (LINE )
348
- if not self .self_hosted :
349
- logger .info (info ('MODE: Using the Modmail API' ))
350
- logger .info (LINE )
351
- await self .validate_api_token ()
352
- logger .info (LINE )
353
- else :
354
- logger .info (info ('Mode: Self-hosting logs.' ))
355
- await self .validate_database_connection ()
356
- logger .info (LINE )
335
+ await self .validate_database_connection ()
336
+ logger .info (LINE )
357
337
logger .info (info ('Connected to gateway.' ))
358
338
359
339
await self .config .refresh ()
@@ -784,32 +764,6 @@ def overwrites(ctx):
784
764
)
785
765
return overwrites
786
766
787
- async def validate_api_token (self ):
788
- try :
789
- self .config .modmail_api_token
790
- except KeyError :
791
- logger .critical (error (f'MODMAIL_API_TOKEN not found.' ))
792
- logger .critical (error ('Set a config variable called '
793
- 'MODMAIL_API_TOKEN with a token from '
794
- 'https://dashboard.modmail.tk.' ))
795
- logger .critical (error ('If you want to self-host logs, '
796
- 'input a MONGO_URI config variable.' ))
797
- logger .critical (error ('A Modmail API token is not needed '
798
- 'if you are self-hosting logs.' ))
799
-
800
- return await self .logout ()
801
- else :
802
- valid = await self .api .validate_token ()
803
- if not valid :
804
- logger .critical (error ('Invalid MODMAIL_API_TOKEN - get one '
805
- 'from https://dashboard.modmail.tk' ))
806
- return await self .logout ()
807
-
808
- user = await self .api .get_user_info ()
809
- username = user ['user' ]['username' ]
810
- logger .info (info ('Validated token.' ))
811
- logger .info (info ('GitHub user: ' + username ))
812
-
813
767
async def validate_database_connection (self ):
814
768
try :
815
769
await self .db .command ('buildinfo' )
@@ -821,32 +775,6 @@ async def validate_database_connection(self):
821
775
else :
822
776
logger .info (info ('Successfully connected to the database.' ))
823
777
824
- async def data_loop (self ):
825
- await self .wait_until_ready ()
826
- self .owner = (await self .application_info ()).owner
827
-
828
- while not self .is_closed ():
829
- data = {
830
- "owner_name" : str (self .owner ),
831
- "owner_id" : self .owner .id ,
832
- "bot_id" : self .user .id ,
833
- "bot_name" : str (self .user ),
834
- "avatar_url" : self .user .avatar_url ,
835
- "guild_id" : self .guild_id ,
836
- "guild_name" : self .guild .name ,
837
- "member_count" : len (self .guild .members ),
838
- "uptime" : (datetime .utcnow () -
839
- self .start_time ).total_seconds (),
840
- "latency" : f'{ self .ws .latency * 1000 :.4f} ' ,
841
- "version" : self .version ,
842
- # TODO: change to `self_hosted`
843
- "selfhosted" : self .self_hosted ,
844
- "last_updated" : str (datetime .utcnow ())
845
- }
846
-
847
- await self .api .post_metadata (data )
848
- await asyncio .sleep (3600 )
849
-
850
778
async def autoupdate_loop (self ):
851
779
await self .wait_until_ready ()
852
780
@@ -855,16 +783,19 @@ async def autoupdate_loop(self):
855
783
logger .info (LINE )
856
784
return
857
785
858
- if self . self_hosted and not self .config .get ('github_access_token' ):
786
+ if not self .config .get ('github_access_token' ):
859
787
logger .warning (info ('GitHub access token not found.' ))
860
788
logger .warning (info ('Autoupdates disabled.' ))
861
789
logger .info (LINE )
862
790
return
863
791
792
+ logger .info (info ('Autoupdate loop started.' ))
793
+
864
794
while not self .is_closed ():
865
- metadata = await self .api .get_metadata ()
795
+ changelog = await Changelog .from_url (self )
796
+ latest = changelog .latest_version
866
797
867
- if metadata [ 'latest_version' ] != self .version :
798
+ if parse_version ( self . version ) < parse_version ( latest .version ) :
868
799
data = await self .api .update_repository ()
869
800
870
801
embed = discord .Embed (color = discord .Color .green ())
@@ -875,10 +806,8 @@ async def autoupdate_loop(self):
875
806
icon_url = user ['avatar_url' ],
876
807
url = user ['url' ])
877
808
embed .set_footer (text = f"Updating Modmail v{ self .version } "
878
- f"-> v{ metadata [ 'latest_version' ] } " )
809
+ f"-> v{ latest . version } " )
879
810
880
- changelog = await Changelog .from_url (self )
881
- latest = changelog .latest_version
882
811
embed .description = latest .description
883
812
for name , value in latest .fields .items ():
884
813
embed .add_field (name = name , value = value )
0 commit comments