forked from techlover1/PugBot-for-Discord
-
Notifications
You must be signed in to change notification settings - Fork 3
/
unban.py
125 lines (114 loc) · 4.44 KB
/
unban.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
# Ban Hammer Bot for use with Pickup-Bot-For-Discord
# Created by: Alex Laswell
import asyncio
import config
import discord
from discord import Game
from discord.ext.commands import Bot
import pymongo
import sys
# Configurable constants
adminChannelID = config.adminChannelID
adminRoleMention = config.adminRoleMention
bannedChannelID = config.bannedChannelID
cmdprefix = config.cmdprefix
dbtoken = config.dbtoken
discordServerID = config.discordServerID
playerRoleID = config.playerRoleID
probationRoleID = config.probationRoleID
timeoutRoleID = config.timeoutRoleID
token = config.banHammerToken
# Setup the required objects
Bot = Bot(command_prefix=cmdprefix)
client = discord.Client()
debug = "false"
server = None
adminRole = None
accessRole = None
poolRole = None
probationRole = None
timeoutRole = None
# create the MongoDB client and connect to the database
dbclient = pymongo.MongoClient(dbtoken)
database = dbclient.FortressForever
# Unban a user from the discord server
async def unban(userID):
global adminChannelID, accessRole, bannedChannelID, database, dbclient, debug, probationRole, server, timeoutRole
tries = 0
while tries < 3:
try:
member = server.get_member(userID)
if debug != "false":
print("DEBUG (" + str(tries) + "): " + str(member))
try:
# delete the ban from the MongoDB
deleted = database.banned.delete_many({"userid": userID})
if deleted.deleted_count > 0:
# give user back access
try:
await member.remove_roles(timeoutRole)
await asyncio.sleep(2)
await member.add_roles(accessRole, probationRole)
await asyncio.sleep(2)
except Exception:
pass
if debug == "false":
# notify the admins and banned users
emb = discord.Embed(
description="The ban for user "
+ member.mention
+ " has been removed.\nThe time period has expired",
colour=0x00FF00,
)
emb.set_author(name=Bot.user.name, icon_url=Bot.user.avatar_url)
await server.get_channel(bannedChannelID).send(embed=emb)
await server.get_channel(adminChannelID).send(embed=emb)
else:
print(
"DEBUG ("
+ str(tries)
+ "): The ban for user "
+ str(member)
+ " has been removed."
)
if debug != "false":
print(
"DEBUG ("
+ str(tries)
+ "): No ban for user "
+ str(member)
+ " found in database."
)
tries = 666
except StopIteration:
if debug != "false":
print(
"DEBUG ("
+ str(tries)
+ "): No ban for user "
+ str(member)
+ " found in database."
)
tries = 666
except Exception as inst:
if debug != "false":
print("DEBUG (" + str(tries) + "): " + str(inst))
tries += 1
pass # ignore all errors and try again (3x total)
dbclient.close()
# when ready the bot removes the ban from the mongoDB and gives the user access again
@Bot.event
async def on_ready():
global adminRole, accessRole, debug, probationRole, probationRoleID, playerRoleID, server, timeoutRole, timeoutRoleID
await Bot.wait_until_ready()
await Bot.change_presence(activity=Game(name="Unbanning " + sys.argv[1]))
server = Bot.get_guild(id=discordServerID)
accessRole = discord.utils.get(server.roles, id=playerRoleID)
probationRole = discord.utils.get(server.roles, id=probationRoleID)
timeoutRole = discord.utils.get(server.roles, id=timeoutRoleID)
if len(sys.argv) > 2:
debug = sys.argv[2]
# unban the userID specified on the commandline
await unban(int(sys.argv[1], base=10))
sys.exit(0)
Bot.run(token)