Skip to content

Commit b6544bb

Browse files
authored
Merge pull request #19 from pythonfoo/discord
added discord support
2 parents 2142b5c + 81e59e5 commit b6544bb

7 files changed

Lines changed: 72 additions & 8 deletions

File tree

‎clients/DiscordClient.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import config
2+
import logging
3+
import requests
4+
5+
log = logging.getLogger(__name__)
6+
7+
8+
class DiscordClient:
9+
def __init__(self):
10+
self.URL = 'https://discord.com/api/v9/channels/{}/messages'.format(config.DISCORD_CHANNEL_ID)
11+
12+
def send_message(self, message):
13+
header = {
14+
'authorization': 'Bot ' + config.DISCORD_TOKEN,
15+
}
16+
17+
payload = {
18+
'content': message,
19+
}
20+
21+
requests.post(self.URL, data=payload, headers=header)
File renamed without changes.

‎clients/__init__.py‎

Whitespace-only changes.

‎config.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
MASTODON_ACCESS_TOKEN = ''
3131
MASTODON_API_BASE_URL = '' # https://botsin.space
3232

33+
DISCORD_DRY_RUN = False
34+
DISCORD_APP_ID = ''
35+
DISCORD_TOKEN = ''
36+
DISCORD_CHANNEL_ID = ''
37+
3338
USER_NAME_BLACK_LIST = set()
3439

3540

‎helper.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,13 @@ def can_matrix():
4545
]
4646

4747
return data_valid(required_config_data)
48+
49+
50+
def can_discord():
51+
required_config_data = [
52+
config.DISCORD_APP_ID,
53+
config.DISCORD_TOKEN,
54+
config.DISCORD_CHANNEL_ID,
55+
]
56+
57+
return data_valid(required_config_data)

‎messager.py‎

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/usr/bin/env python3
22
import logging
33
from twython import Twython
4-
from MatrixClient import MatrixClient
4+
from clients.MatrixClient import MatrixClient
5+
from clients.DiscordClient import DiscordClient
56
from mastodon import Mastodon
67
import config
78
import 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+
7282
def 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+
171198
if __name__ == '__main__':
172199
can_message = CanMessage.CanMessage()
173200

‎requirements.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ mwclient
22
twython
33
matrix-nio
44
Mastodon.py
5+
requests

0 commit comments

Comments
 (0)