@@ -72,6 +72,7 @@ class DiscordPlugin(BasePlugin, discord.Client):
7272 name = "discord_bot"
7373 depends = ['command_dispatcher' ]
7474 default_config = {
75+ "enabled" : True ,
7576 "token" : "-- token --" ,
7677 "client_id" : "-- client_id --" ,
7778 "channel" : "-- channel id --" ,
@@ -87,8 +88,11 @@ class DiscordPlugin(BasePlugin, discord.Client):
8788 def __init__ (self ):
8889 BasePlugin .__init__ (self )
8990 discord .Client .__init__ (self )
91+ self .enabled = True
9092 self .token = None
93+ self .channel_id = None
9194 self .channel = None
95+ self .staff_channel_id = None
9296 self .staff_channel = None
9397 self .token = None
9498 self .client_id = None
@@ -111,6 +115,9 @@ def __init__(self):
111115 'shutdown' , 'save' )
112116
113117 def activate (self ):
118+ self .enabled = self .config .get_plugin_config (self .name )["enabled" ]
119+ if not self .enabled :
120+ return ;
114121 BasePlugin .activate (self )
115122 self .dispatcher = self .plugins .command_dispatcher
116123 self .irc_bot_exists = link_plugin_if_available (self , 'irc_bot' )
@@ -122,11 +129,11 @@ def activate(self):
122129 "command_prefix" ]
123130 self .token = self .config .get_plugin_config (self .name )["token" ]
124131 self .client_id = self .config .get_plugin_config (self .name )["client_id" ]
125- self .channel = self .config .get_plugin_config (self .name )["channel" ]
126- self .staff_channel = self .config .get_plugin_config (self .name )[
132+ self .channel_id = self .config .get_plugin_config (self .name )["channel" ]
133+ self .staff_channel_id = self .config .get_plugin_config (self .name )[
127134 "staff_channel" ]
128135 self .sc = self .config .get_plugin_config (self .name )["strip_colors" ]
129- asyncio .ensure_future (self .start_bot ())
136+ asyncio .ensure_future (self .start_bot ()). add_done_callback ( self . error_handler )
130137 self .update_id (self .client_id )
131138 self .mock_connection = MockConnection (self )
132139 self .rank_roles = self .config .get_plugin_config (self .name )[
@@ -151,7 +158,9 @@ def on_connect_success(self, data, connection):
151158 :param connection:
152159 :return: Boolean: True. Must be true, so packet moves on.
153160 """
154- asyncio .ensure_future (self .make_announce (connection , "joined" ))
161+ if not self .enabled :
162+ return True ;
163+ asyncio .ensure_future (self .make_announce (connection , "joined" )).add_done_callback (self .error_handler )
155164 return True
156165
157166 def on_client_disconnect_request (self , data , connection ):
@@ -162,7 +171,9 @@ def on_client_disconnect_request(self, data, connection):
162171 :param connection:
163172 :return: Boolean: True. Must be true, so packet moves on.
164173 """
165- asyncio .ensure_future (self .make_announce (connection , "left" ))
174+ if not self .enabled :
175+ return True ;
176+ asyncio .ensure_future (self .make_announce (connection , "left" )).add_done_callback (self .error_handler )
166177 return True
167178
168179 def on_chat_sent (self , data , connection ):
@@ -177,6 +188,8 @@ def on_chat_sent(self, data, connection):
177188 :param connection:
178189 :return: Boolean: True. Must be true, so packet moves on.
179190 """
191+ if not self .enabled :
192+ return True ;
180193 if not data ["parsed" ]["message" ].startswith (self .prefix ):
181194 msg = data ["parsed" ]["message" ]
182195 if self .sc :
@@ -207,13 +220,14 @@ def start_bot(self):
207220 except Exception as e :
208221 self .logger .exception (e )
209222
223+
210224 def update_id (self , client_id ):
211225 self .client_id = client_id
212226
213227 @asyncio .coroutine
214228 def on_ready (self ):
215- self .channel = self .get_channel (self .channel )
216- self .staff_channel = self .get_channel (self .staff_channel )
229+ self .channel = self .get_channel (self .channel_id )
230+ self .staff_channel = self .get_channel (self .staff_channel_id )
217231 if not self .channel :
218232 self .logger .error ("Couldn't get channel! Messages can't be "
219233 "sent! Ensure the channel ID is correct." )
@@ -305,4 +319,12 @@ def bot_write(self, msg, target=None):
305319 target = self .channel
306320 if target is None :
307321 return
308- asyncio .ensure_future (self .send_message (target , msg ))
322+ asyncio .ensure_future (self .send_message (target , msg )).add_done_callback (self .error_handler )
323+
324+ def error_handler (self , future ):
325+ try :
326+ future .result ()
327+ except Exception as e :
328+ self .logger .error ("Caught an unhandled exception in Discord bot. Will restart." )
329+ self .logger .exception (e )
330+ asyncio .ensure_future (self .start_bot ())
0 commit comments