-
Notifications
You must be signed in to change notification settings - Fork 0
/
discordd.py
87 lines (64 loc) · 1.95 KB
/
discordd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import logging
import discord
import asyncio
from asyncio import coroutines
import concurrent.futures
from asyncio import futures
from discord import channel
logging.basicConfig(level=logging.INFO)
thread_lock = None
config = None
client = discord.Client()
server = None
channels = {}
irc = None
class Discord:
def __init__(self, conf):
global config
global thread_lock
global channels
channels = {conf['CHANNELS'][channel]
: channel for channel in conf['CHANNELS']}
config = conf
def set_irc(self, ircd):
global irc
irc = ircd
def set_thread_lock(self, lock):
global thread_lock
thread_lock = lock
def privmsg(self, target, message):
global client
asyncio.run_coroutine_threadsafe(
async_privmsg(target, message), client.loop)
def run(self):
global config
global client
client.run(config["TOKEN"])
def close(self):
global client
global irc
irc.set_running(False)
asyncio.run_coroutine_threadsafe(client.close(), client.loop)
async def async_privmsg(target, message):
global client
channel = client.get_channel(target)
await channel.send(message.strip())
@client.event
async def on_message(message):
global config
global client
global channels
global thread_lock
global irc
# Don't reply to itself
if message.author == client.user:
return
with thread_lock:
content = message.clean_content
if len(message.attachments) > 0:
content += ' ' + message.attachments[0].url
if content.startswith('+') or content.startswith('-') or content.startswith('!') or content.startswith('@') or content.startswith('.'):
irc.privmsg(channels[message.channel.id], "%s" % (content))
else:
irc.privmsg(channels[message.channel.id], "<%s> %s" %
(message.author.display_name, content))