11#!/usr/bin/env python3
22import logging
33from twython import Twython
4- from MatrixClient import MatrixClient
4+ from clients .MatrixClient import MatrixClient
5+ from clients .DiscordClient import DiscordClient
56from mastodon import Mastodon
67import config
78import helper
@@ -69,16 +70,26 @@ def get_bot_mastodon() -> Mastodon() or None:
6970 return mastodon_bot
7071
7172
73+ def get_bot_discord () -> DiscordClient () or None :
74+ if not helper .can_discord ():
75+ return None
76+
77+ log .debug ("initialise discord client" )
78+
79+ return DiscordClient ()
80+
81+
7282def run (last_revid ):
7383 """
74- Tweets less than 'max_changes' changes newer than 'last_revid'.
84+ Sends messages less than 'max_changes' changes newer than 'last_revid'.
7585 Returns the new 'last_revid'.
7686 """
7787 log .debug ("last rev id: %s" , last_revid )
7888
7989 twitter_bot = get_bot_twitter ()
8090 matrix_bot = get_bot_matrix ()
8191 mastodon_bot = get_bot_mastodon ()
92+ discord_bot = get_bot_discord ()
8293
8394 changes = list ()
8495 changes_overflow = False
@@ -111,9 +122,10 @@ def run(last_revid):
111122
112123 for change in changes :
113124 message = change .get_message ()
114- tweet (twitter_bot , message )
115- toot (mastodon_bot , message )
116- matrix (matrix_bot , message )
125+ send_tweet (twitter_bot , message )
126+ send_toot (mastodon_bot , message )
127+ send_matrix (matrix_bot , message )
128+ send_discord (discord_bot , message )
117129
118130 if not changes :
119131 log .debug ("no changes, rev id: %s" , last_revid )
@@ -123,7 +135,7 @@ def run(last_revid):
123135 return changes [- 1 ].revId
124136
125137
126- def tweet (twitter , message ):
138+ def send_tweet (twitter , message ):
127139 if twitter is None :
128140 return
129141
@@ -138,7 +150,7 @@ def tweet(twitter, message):
138150 log .warning ('TWITTER DRY RUN!' )
139151
140152
141- def toot (_mastodon , message ):
153+ def send_toot (_mastodon , message ):
142154 if _mastodon is None :
143155 return
144156
@@ -153,7 +165,7 @@ def toot(_mastodon, message):
153165 log .warning ('MASTODON DRY RUN!' )
154166
155167
156- def matrix (_matrix , message ):
168+ def send_matrix (_matrix , message ):
157169 if _matrix is None :
158170 return
159171
@@ -168,6 +180,21 @@ def matrix(_matrix, message):
168180 log .warning ('MATRIX DRY RUN!' )
169181
170182
183+ def send_discord (_discord , message ):
184+ if _discord is None :
185+ return
186+
187+ logging .debug ("Send discord: %s" , message )
188+
189+ if not config .DISCORD_DRY_RUN :
190+ try :
191+ _discord .send_message (message )
192+ except Exception as ex :
193+ log .error ('DISCORD SEND ERROR: ' + str (ex ))
194+ else :
195+ log .warning ('DISCORD DRY RUN!' )
196+
197+
171198if __name__ == '__main__' :
172199 can_message = CanMessage .CanMessage ()
173200
0 commit comments