forked from gradiuscypher/discord_gradiusbot
/
gradiusbot.py
executable file
·134 lines (108 loc) · 4.82 KB
/
gradiusbot.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
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
#! venv3/bin/python
from plugin_loader import PluginLoader
import discord
import asyncio
import configparser
import traceback
import json
import sys
import logging
import twitter
config = configparser.RawConfigParser()
plugins = PluginLoader()
client = discord.Client()
@client.async_event
def on_ready():
print('Logged in as: ', client.user.name, 'with ID:', client.user.id)
@client.async_event
def on_message(message):
selfname = config.get("BotSettings", "self_name")
permitted_channels = json.loads(config.get('BotSettings', 'permitted_channels'))
server_id = config.get("BotSettings", "server_id")
if message.channel.is_private:
if not message.author.name == selfname:
for plugin in plugins.private_plugins:
try:
asyncio.async(plugin.action(message, client, config))
except:
print("There was an error with: " + str(plugin))
print(traceback.format_exc())
else:
if (message.server.id == server_id) or (server_id == ""):
if not message.author.name == selfname and message.channel.name in permitted_channels:
for plugin in plugins.public_plugins:
try:
asyncio.async(plugin.action(message, client, config))
except:
print("There was an error with: " + str(plugin))
print(traceback.format_exc())
@client.async_event
def on_message_delete(message):
selfname = config.get("BotSettings", "self_name")
permitted_channels = json.loads(config.get('BotSettings', 'permitted_channels'))
server_id = config.get("BotSettings", "server_id")
if message.channel.is_private:
if not message.author.name == selfname:
# TODO: Implement for private messages
pass
else:
if (message.server.id == server_id) or (server_id == ""):
if not message.author.name == selfname and message.channel.name in permitted_channels:
for plugin in plugins.event_plugins:
try:
asyncio.async(plugin.action(message, client, config, "delete"))
except:
print("There was an error with: " + str(plugin))
print(traceback.format_exc())
@client.async_event
def on_message_edit(message, message_after):
selfname = config.get("BotSettings", "self_name")
permitted_channels = json.loads(config.get('BotSettings', 'permitted_channels'))
server_id = config.get("BotSettings", "server_id")
if message.channel.is_private:
if not message.author.name == selfname:
# TODO: Implement for private messages
pass
else:
if (message.server.id == server_id) or (server_id == ""):
if not message.author.name == selfname and message.channel.name in permitted_channels:
for plugin in plugins.event_plugins:
try:
asyncio.async(plugin.action(message, client, config, "edit", object_after=message_after))
except:
print("There was an error with: " + str(plugin))
print(traceback.format_exc())
@client.async_event
def on_member_update(member_before, member_after):
server_id = config.get("BotSettings", "server_id")
if (member_after.server.id == server_id) or (server_id == ""):
for plugin in plugins.event_plugins:
try:
asyncio.async(plugin.action(member_before, client, config, "member_update", object_after=member_after))
except:
print("There was an error with: " + str(plugin))
print(traceback.format_exc())
def main_task(config_file):
config.read(config_file)
email = config.get("Account", "email")
password = config.get("Account", "password")
if config.getboolean("BotSettings", "debug"):
logging.basicConfig(level=logging.DEBUG)
plugins.load_plugins(config)
try:
print("Running client...")
client.run(email, password)
except KeyboardInterrupt:
print("Killed by keboard!")
except:
print("There was an exception:")
print(traceback.print_exc())
if config.getboolean("BotSettings", "twitter_alert"):
access_key = config.get("Twitter", "access_key")
access_secret = config.get("Twitter", "access_secret")
consumer_key = config.get("Twitter", "consumer_key")
consumer_secret = config.get("Twitter", "consumer_secret")
tapi = twitter.Twitter(auth=twitter.OAuth(access_key, access_secret, consumer_key, consumer_secret))
tapi.statuses.update(status="@RiotGradius, I'm no longer in Discord.")
if __name__ == "__main__":
main_task(sys.argv[1])