|
1
|
|
|
# Copyright 2017 Starbot Discord Project |
|
2
|
|
|
# |
|
3
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); |
|
4
|
|
|
# you may not use this file except in compliance with the License. |
|
5
|
|
|
# You may obtain a copy of the License at |
|
6
|
|
|
# |
|
7
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
|
8
|
|
|
# |
|
9
|
|
|
# Unless required by applicable law or agreed to in writing, software |
|
10
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
|
11
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
12
|
|
|
# See the License for the specific language governing permissions and |
|
13
|
|
|
# limitations under the License. |
|
14
|
|
|
|
|
15
|
|
|
import glob |
|
16
|
|
|
import importlib |
|
17
|
|
|
import time |
|
18
|
|
|
import sys |
|
19
|
|
|
import asyncio |
|
20
|
|
|
|
|
21
|
|
|
import discord |
|
22
|
|
|
from pluginbase import PluginBase |
|
23
|
|
|
|
|
24
|
|
|
from api import settings, message, logging |
|
25
|
|
|
from api import command as command_api |
|
26
|
|
|
from api.bot import Bot |
|
27
|
|
|
from libs import displayname |
|
28
|
|
|
|
|
29
|
|
|
def initPlugin(plugin, autoImport=True): |
|
30
|
|
|
# Init plugin. |
|
31
|
|
|
if autoImport == True: |
|
32
|
|
|
plugin_temp = plugin_source.load_plugin(plugin) |
|
33
|
|
|
plugin_info = plugin_temp.onInit(plugin_temp) |
|
34
|
|
|
else: |
|
35
|
|
|
plugin_info = plugin.onInit(plugin) |
|
36
|
|
|
|
|
37
|
|
|
# Verify the plugin is defined, it has a name, and it has commands. |
|
38
|
|
|
if plugin_info.plugin == None: |
|
39
|
|
|
print("Plugin not defined!") |
|
40
|
|
|
pass |
|
41
|
|
|
if plugin_info.name == None: |
|
42
|
|
|
print("Plugin name not defined") |
|
43
|
|
|
pass |
|
44
|
|
|
if plugin_info.commands == []: |
|
45
|
|
|
print("Plugin did not define any commands.") |
|
46
|
|
|
pass |
|
47
|
|
|
|
|
48
|
|
|
# Add plugin to list. |
|
49
|
|
|
Bot.plugins.append(plugin_info) |
|
50
|
|
|
|
|
51
|
|
|
# Load each command in plugin. |
|
52
|
|
|
for command in plugin_info.commands: |
|
53
|
|
|
# Verify command has a parent plugin and a name. |
|
54
|
|
|
if command.plugin == None: |
|
55
|
|
|
print("Plugin command does not define parent plugin") |
|
56
|
|
|
pass |
|
57
|
|
|
if command.name == None: |
|
58
|
|
|
print("Plugin command does not define name") |
|
59
|
|
|
pass |
|
60
|
|
|
|
|
61
|
|
|
# Add command to list of commands and print a success message. |
|
62
|
|
|
Bot.commands.append(command) |
|
63
|
|
|
print("Command `{}` registered successfully.".format(command.name)) |
|
64
|
|
|
|
|
65
|
|
|
# Print success message. |
|
66
|
|
|
print("Plugin '{}' registered successfully.".format(plugin_info.name)) |
|
67
|
|
|
|
|
68
|
|
|
class FakeClient: |
|
69
|
|
|
def event(self): |
|
70
|
|
|
pass |
|
71
|
|
|
|
|
72
|
|
|
if __name__ == "__main__": |
|
73
|
|
|
from api import database |
|
74
|
|
|
database.init() |
|
75
|
|
|
|
|
76
|
|
|
# Log the time we started. |
|
77
|
|
|
Bot.startTime = time.time() |
|
78
|
|
|
|
|
79
|
|
|
# Get the source of plugins. |
|
80
|
|
|
plugin_base = PluginBase(package="plugins") |
|
81
|
|
|
plugin_source = plugin_base.make_plugin_source(searchpath=["./plugins"]) |
|
82
|
|
|
|
|
83
|
|
|
# Create the Discord client. |
|
84
|
|
|
client = discord.Client() |
|
85
|
|
|
Bot.client = client |
|
86
|
|
|
|
|
87
|
|
|
# Load each plugin. |
|
88
|
|
|
for plugin in plugin_source.list_plugins(): |
|
89
|
|
|
initPlugin(plugin) |
|
90
|
|
|
|
|
91
|
|
|
# Get our token to use. |
|
92
|
|
|
token = "" |
|
93
|
|
|
with open("token.txt") as m: |
|
94
|
|
|
token = m.read().strip() |
|
95
|
|
|
else: |
|
96
|
|
|
client = discord.Client() |
|
97
|
|
|
Bot.client = client |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
@client.event |
|
101
|
|
|
async def on_ready(): |
|
102
|
|
|
# Print logged in message to console. |
|
103
|
|
|
print("Logged in as") |
|
104
|
|
|
print(client.user.name) |
|
105
|
|
|
print(client.user.id) |
|
106
|
|
|
print("------") |
|
107
|
|
|
print("Bot Invite Link: " + "https://discordapp.com/oauth2/authorize?client_id=" + str(client.user.id) + "&scope=bot&permissions=8") |
|
108
|
|
|
print("------") |
|
109
|
|
|
|
|
110
|
|
|
# Set the game. |
|
111
|
|
|
await client.change_presence(activity=discord.Game(name="with magic")) |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
@client.event |
|
115
|
|
|
async def on_message(message_in): |
|
116
|
|
|
# Ignore messages that aren't from a server and from ourself. |
|
117
|
|
|
#if not message_in.guild: |
|
118
|
|
|
# return |
|
119
|
|
|
if message_in.author.id == client.user.id: |
|
120
|
|
|
return |
|
121
|
|
|
if message_in.author.bot: |
|
122
|
|
|
return |
|
123
|
|
|
|
|
124
|
|
|
is_command = False |
|
125
|
|
|
|
|
126
|
|
|
# Get prefix. If not on a server, no prefix is needed. |
|
127
|
|
|
#logging.message_log(message_in, message_in.guild.id) |
|
128
|
|
|
if message_in.guild: |
|
129
|
|
|
prefix = settings.prefix_get(message_in.guild.id) |
|
130
|
|
|
me = message_in.guild.me |
|
131
|
|
|
else: |
|
132
|
|
|
prefix = "" |
|
133
|
|
|
me = message_in.channel.me |
|
134
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
# Should we die? Check for exit command. |
|
137
|
|
|
if message_in.content == prefix + "exit" or message_in.content == "{} exit".format(me.mention): |
|
138
|
|
|
if settings.owners_check(message_in.author.id): |
|
139
|
|
|
sys.exit(0) |
|
140
|
|
|
|
|
141
|
|
|
# Check for cache contents command. |
|
142
|
|
|
if message_in.content.startswith(prefix + "cachecontents") or message_in.content.startswith("{} cachecontents".format(me.mention)): |
|
143
|
|
|
cacheCount = glob.glob("cache/{}_*".format(message_in.content.split(' ')[-1])) |
|
144
|
|
|
cacheString = '\n'.join(cacheCount) |
|
145
|
|
|
await message_in.channel.send(message_in.channel, "```{}```".format(cacheString)) |
|
146
|
|
|
|
|
147
|
|
|
# Check each command loaded. |
|
148
|
|
|
for command in Bot.commands: |
|
149
|
|
|
# Do we have a command? |
|
150
|
|
|
if command_api.is_command(message_in, prefix, command): |
|
151
|
|
|
# Prevent message count increment. |
|
152
|
|
|
is_command = True |
|
153
|
|
|
|
|
154
|
|
|
# Send typing message. |
|
155
|
|
|
async with message_in.channel.typing(): |
|
156
|
|
|
# Build message object. |
|
157
|
|
|
message_recv = message.Message |
|
158
|
|
|
message_recv.command = command.name |
|
159
|
|
|
if message_in.content.startswith("{} ".format(me.mention)): |
|
160
|
|
|
message_recv.body = message_in.content.split("{} ".format(me.mention) + |
|
161
|
|
|
command.name, 1)[1] |
|
162
|
|
|
else: |
|
163
|
|
|
message_recv.body = message_in.content.split(prefix + command.name, 1)[1] |
|
164
|
|
|
message_recv.author = message_in.author |
|
165
|
|
|
message_recv.guild = message_in.guild |
|
166
|
|
|
message_recv.mentions = message_in.mentions |
|
167
|
|
|
message_recv.channel = message_in.channel |
|
168
|
|
|
|
|
169
|
|
|
command_result = await command.func(message_recv) |
|
170
|
|
|
|
|
171
|
|
|
# No message, error. |
|
172
|
|
|
if not command_result: |
|
173
|
|
|
await message_in.channel.send( |
|
174
|
|
|
"**Beep boop - Something went wrong!**\n_Command did not return a result._") |
|
175
|
|
|
|
|
176
|
|
|
# Do list of messages, one after the other. If the message is more than 5 chunks long, PM it. |
|
177
|
|
|
elif type(command_result) is list: |
|
178
|
|
|
if len(command_result) > 5: # PM messages. |
|
179
|
|
|
# Send message saying that we are PMing the messages. |
|
180
|
|
|
await message_in.channel.send( |
|
181
|
|
|
"Because the output of that command is **{} pages** long, I'm just going to PM the result to you.".format(len(command_result))) |
|
182
|
|
|
|
|
183
|
|
|
# PM it. |
|
184
|
|
|
for item in command_result: |
|
185
|
|
|
await process_message(message_in.author, message_in, item) |
|
186
|
|
|
|
|
187
|
|
|
else: # Send to channel. |
|
188
|
|
|
for item in command_result: |
|
189
|
|
|
await process_message(message_in.channel, message_in, item) |
|
190
|
|
|
|
|
191
|
|
|
# Do regular message. |
|
192
|
|
|
else: |
|
193
|
|
|
await process_message(message_in.channel, message_in, command_result) |
|
194
|
|
|
|
|
195
|
|
|
# Do we delete the message afterwards? |
|
196
|
|
|
if message_in.guild and command_result.delete: |
|
197
|
|
|
await client.delete_message(message_in) |
|
198
|
|
|
|
|
199
|
|
|
# Increment message counters if not command. |
|
200
|
|
|
if message_in.guild and not is_command: |
|
201
|
|
|
logging.message_log(message_in, message_in.guild.id) |
|
202
|
|
|
count = logging.message_count_get(message_in.guild.id) |
|
203
|
|
|
Bot.messagesSinceStart += 1 |
|
204
|
|
|
count += 1 |
|
205
|
|
|
|
|
206
|
|
|
async def process_message(target, message_in, msg): |
|
207
|
|
|
# If the message to send has a body |
|
208
|
|
|
if msg.body: |
|
209
|
|
|
# Remove @everyone and @here from messages. |
|
210
|
|
|
zerospace = "" |
|
211
|
|
|
msg.body = msg.body.replace("@everyone", "@{}everyone".format(zerospace)).replace("@here", "@{}here".format(zerospace)) |
|
212
|
|
|
|
|
213
|
|
|
# If the message to send includes a file |
|
214
|
|
|
if msg.file != "": |
|
215
|
|
|
# Send the file, along with any possible message |
|
216
|
|
|
await target.send(msg.body, embed=msg.embed, file=msg.file) |
|
217
|
|
|
else: |
|
218
|
|
|
# Send the message, along with a possible embed |
|
219
|
|
|
await target.send(msg.body, embed=msg.embed) |
|
220
|
|
|
|
|
221
|
|
|
if __name__ == "__main__": |
|
222
|
|
|
# Start bot. |
|
223
|
|
|
client.run(token) |
|
|
|
|
|
|
224
|
|
|
|