-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdiscordc.py
More file actions
149 lines (112 loc) · 4.47 KB
/
discordc.py
File metadata and controls
149 lines (112 loc) · 4.47 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import logging
import discord
import asyncio
from asyncio import coroutines
import concurrent.futures
from asyncio import futures
logging.basicConfig(level=logging.INFO)
thread_lock = None
settings = None
client = discord.Client()
server = None
channel = None
irc = None
class Discord:
def __init__(self, sett):
global settings
global thread_lock
settings = sett["discord"]
if not settings["token"]:
with thread_lock:
print("[Discord] No token given. Get a token at https://discordapp.com/developers/applications/me")
exit()
def set_irc(self, ircc):
global irc
irc = ircc
def set_thread_lock(self, lock):
global thread_lock
thread_lock = lock
def send_my_message(self, message):
global client
asyncio.run_coroutine_threadsafe(send_my_message_async(message), client.loop)
def run(self):
global settings
global client
client.run(settings["token"])
def close(self):
global client
global irc
irc.set_running(False)
asyncio.run_coroutine_threadsafe(client.close(), client.loop)
async def send_my_message_async(message):
await channel.send(message.strip())
@client.event
async def on_message(message):
global settings
global client
global channel
global thread_lock
global irc
# Don't reply to itself
if message.author == client.user:
return
if message.channel != channel:
return
if message.author.name == settings["botowner"]:
if message.content.strip() == "!quit":
await client.close()
return
with thread_lock:
print("[Discord] %s: %s" % (message.author.name, message.content.strip()))
content = message.clean_content
if len(message.attachments) > 0:
content += ' ' + message.attachments[0].url
irc.send_my_message("%s: %s" % (message.author.name, content))
@client.event
async def on_ready():
global server
global channel
global thread_lock
with thread_lock:
print("[Discord] Logged in as:")
print("[Discord] " + client.user.name)
print("[Discord] " + str(client.user.id))
if len(client.guilds) == 0:
print("[Discord] Bot is not yet in any server.")
await client.close()
return
if settings["server"] == "":
print("[Discord] You have not configured a server to use in settings.json")
print("[Discord] Please put one of the server IDs listed below in settings.json")
for server in client.guilds:
print("[Discord] %s: %s" % (server.name, server.id))
await client.close()
return
findServer = [x for x in client.guilds if str(x.id) == settings["server"]]
if not len(findServer):
print("[Discord] No server could be found with the specified id: " + settings["server"])
print("[Discord] Available servers:")
for server in client.guilds:
print("[Discord] %s: %s" % (server.name, server.id))
await client.close()
return
server = findServer[0]
if settings["channel"] == "":
print("[Discord] You have not configured a channel to use in settings.json")
print("[Discord] Please put one of the channel IDs listed below in settings.json")
for channel in server.channels:
if channel.type == discord.ChannelType.text:
print("[Discord] %s: %s" % (channel.name, channel.id))
await client.close()
return
findChannel = [x for x in server.channels if str(x.id) == settings["channel"] and x.type == discord.ChannelType.text]
if not len(findChannel):
print("[Discord] No channel could be found with the specified id: " + settings["server"])
print("[Discord] Note that you can only use text channels.")
print("[Discord] Available channels:")
for channel in server.channels:
if channel.type == discord.ChannelType.text:
print("[Discord] %s: %s" % (channel.name, channel.id))
await client.close()
return
channel = findChannel[0]