-
Notifications
You must be signed in to change notification settings - Fork 3
/
role_bot_discord.py
93 lines (73 loc) · 2.97 KB
/
role_bot_discord.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
import sys
import re
import json
import discord
import os
import pickle
import core.auth as conf_auth
import core.bot_base as base
# This bot monitors the unlock channel and hands out roles.
# This bot can be started at the beginning of the conference and left running the entire time.
if(not "DATA_FOLDER" in os.environ):
print("You must set $DATA_FOLDER to a folder which contains the working data of this tool.")
sys.exit(1)
f = open(os.environ["DATA_FOLDER"] + "/discordIDs.dat", "rb")
discordIDs = pickle.load(f)
f.close()
discord_server_id = discordIDs["Server"]
role_channel_id = discordIDs["RoleChannel"]
auth = conf_auth.Authentication()
client = discord.Client()
brute_force_protection_file_name = os.environ["DATA_FOLDER"] + "/brute_force_protection.dat"
# The keys of this dictionary should have the same name as the roles on your Discord.
token_file_names = {"Attendee": os.environ["DATA_FOLDER"] + "/Attendee_Tokens.txt",
"Chair": os.environ["DATA_FOLDER"] + "/Chair_Tokens.txt",
"Speaker": os.environ["DATA_FOLDER"] + "/Speaker_Tokens.txt"}
@client.event
async def on_ready():
guild = [g for g in client.guilds if g.id == discord_server_id][0]
print("Role Bot ready.")
@client.event
async def on_message(msg):
f = open(brute_force_protection_file_name, "rb")
sender_dict = pickle.load(f)
f.close()
user_dict_id = msg.author.name + msg.author.discriminator
if(msg.author == client.user):
return
if(msg.guild.id != discord_server_id or
msg.channel.id != role_channel_id):
return
if(user_dict_id in sender_dict.keys()):
if(sender_dict[user_dict_id] >= 3):
await msg.author.send(content = "You tried to unlock too often. Please contact an administrator for help.")
print("Brute Force Attempt by " + str(msg.author) + " blocked.")
print()
return
print("Role requested by {}".format(msg.author.name))
for group in token_file_names.keys():
f = open(token_file_names[group], "r")
tokens = f.readlines()
f.close()
if(msg.content + "\n" in tokens):
print(str(group) + " token detected.")
tokens.remove(msg.content + "\n")
f = open(token_file_names[group], "w")
f.writelines(tokens)
f.close()
role = discord.utils.get(msg.guild.roles, name = group)
await msg.author.add_roles(role)
await msg.author.send(content = "You received the role " + str(role) + ".")
print(str(msg.author) + " got the role: " + str(role))
print()
return
print("Token not detected.")
print()
if(user_dict_id in sender_dict.keys()):
sender_dict[user_dict_id] += 1
else:
sender_dict[user_dict_id] = 1
f = open(brute_force_protection_file_name, "wb")
pickle.dump(sender_dict, f)
f.close()
client.run(auth.discord["bot_token"])