This repository has been archived by the owner on Sep 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
discordban.py
183 lines (156 loc) · 6.76 KB
/
discordban.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#
# DiscordB3 (www.namelessnoobs.com)
# Copyright (C) 2016 st0rm
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#Credits
#Fenix the orginal author of the irc b3 bot which this plugin is based on.
#Mordecaii from iG for his lovely discordPush fuction <3.
#ItsDizzy from aD for the embedded message and some cleanups.
__author__ = "Fenix, st0rm, Mordecaii, ItsDizzy"
__version__ = "1.2"
import b3
import b3.plugin
import b3.events
import datetime
import urllib2
import json
from b3.functions import minutesStr
class DiscordbanPlugin(b3.plugin.Plugin):
####################################################################################################################
# #
# PLUGIN INIT #
# #
####################################################################################################################
def __init__(self, console, config=None):
"""
Build the plugin object.
:param console: The parser instance.
:param config: The plugin configuration object instance.
"""
b3.plugin.Plugin.__init__(self, console, config)
self.adminPlugin = self.console.getPlugin("admin")
if not self.adminPlugin:
raise AttributeError("could not start without admin plugin")
def onLoadConfig(self):
"""
Load plugin configuration.
"""
self._discordWebhookUrl = self.config.get("authentication","webhookUrl")
self._serverName = self.config.get("authentication","hostname")
def onStartup(self):
"""
Initialize plugin settings.
"""
# register necessary events
self.registerEvent(self.console.getEventID("EVT_CLIENT_BAN"), self.onBan)
self.registerEvent(self.console.getEventID("EVT_CLIENT_BAN_TEMP"), self.onBan)
self.registerEvent(self.console.getEventID("EVT_CLIENT_KICK"), self.onKick)
# notice plugin started
self.debug("plugin started")
####################################################################################################################
# #
# EVENTS #
# #
####################################################################################################################
def onBan(self, event):
"""
Perform operations when EVT_CLIENT_BAN or EVT_CLIENT_BAN_TEMP is received.
:param event: An EVT_CLIENT_BAN or and EVT_CLIENT_BAN_TEMP event.
"""
admin = event.data["admin"]
client = event.client
reason = event.data["reason"]
admin_name = ""
if admin == None:
admin_name = "B3"
else:
admin_name = admin.name
embed = {
"title": "B3 Ban",
"description": "**%s** Banned **%s**" % (admin_name, client.name),
"timestamp": datetime.datetime.now().isoformat(),
"color": 15466496,
"fields": [
{
"name": "Server",
"value": self._serverName,
"inline": False
}
]
}
if reason:
# if there is a reason attached to the ban, append it to the notice
embed["fields"].append({
"name": "Reason",
"value": self.console.stripColors(reason),
"inline": True
})
duration = 'permanent'
if 'duration' in event.data:
# if there is a duration convert it
duration = minutesStr(event.data['duration'])
# append the duration to the ban notice
embed["fields"].append({"name": "Duration", "value": duration, "inline": True})
self.discordEmbeddedPush(embed)
def onKick(self, event):
"""
Perform operations when EVT_CLIENT_KICK is received.
:param event: An EVT_CLIENT_KICK event.
"""
admin = event.data["admin"]
client = event.client
reason = event.data["reason"]
admin_name = ""
if admin == None:
admin_name = "B3"
else:
admin_name = admin.name
embed = {
"title": "B3 Kick",
"description": "**%s** Kicked **%s**" % (admin_name, client.name),
"timestamp": datetime.datetime.now().isoformat(),
"color": 15466496,
"fields": [
{
"name": "Server",
"value": self._serverName,
"inline": False
}
]
}
if reason:
# if there is a reason attached to the ban, append it to the notice
embed["fields"].append({
"name": "Reason",
"value": self.console.stripColors(reason),
"inline": True
})
self.discordEmbeddedPush(embed)
def discordEmbeddedPush(self, embed):
"""
Send embedded message to discord bot huehue
"""
data = json.dumps({"embeds": [embed]})
req = urllib2.Request(self._discordWebhookUrl, data, {
"Content-Type": "application/json",
"User-Agent": "B3DiscordbanPlugin/1.1" #Is that a real User-Agent? Nope but who cares.
})
# Final magic happens here, we will never get an error ofcourse ;)
try:
urllib2.urlopen(req)
except urllib2.HTTPError as ex:
self.debug("Cannot push data to Discord. is your webhook url right?")
self.debug("Data: %s\nCode: %s\nRead: %s" % (data, ex.code, ex.read()))