-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathewb.py
192 lines (147 loc) · 5.27 KB
/
ewb.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
184
185
186
187
188
189
190
191
192
import discord
import os
import json
from discord.ext import commands
from discord.utils import get
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix='^', intents = intents)
@bot.event
async def on_ready():
print("EWB Client has connected :D")
@bot.event
async def on_member_join(member):
role = get(member.guild.roles, name="EWB Members")
await member.add_roles(role)
@bot.command(aliases = ['hello', 'hey you', 'hey', 'oi'])
async def hi(ctx):
await ctx.channel.send("henlo")
@bot.command()
async def bri(ctx):
await ctx.channel.send(ctx.author.mention + " frick")
# admin commands
async def verify(ctx, roleName):
role = get(ctx.message.guild.roles, name = roleName)
if (role in ctx.author.roles):
return True
else:
await ctx.channel.send("You do not have proper permissions to use that command!")
return False
async def warn(ctx, msg):
await ctx.channel.send(ctx.author.mention + " " + msg)
async def attempt_add_reaction(ref, reactions_object, ctx):
for emoji in reactions_object:
try:
await ref.add_reaction(emoji)
except:
try:
emoji_token = bot.get_emoji(id = emoji)
await ref.add_reaction(emoji_token)
except:
await warn(ctx, "Some of those emojis are not allowed!")
return
# TODO(dillon): If this bot scales larger, this command should also search through a "guild" list
# so we can avoid searching through all active_messages in every server each time we run this event.
# This could also be used to potentially throttle servers with too many reaction listeners if necessary.
# Messages with active listeners for emoji reactions
active_messages = []
# Keyed by message_id = []
reaction_list = {}
role_list = {}
async def validate_reaction(message_to_check, user):
for message in active_messages:
if (message_to_check == message):
if (bot.user == user):
return True
return False
@bot.event
async def on_raw_reaction_add(payload):
roleIndex = 0
for message in active_messages:
if (payload.message_id == message.id):
for reaction_emoji in reaction_list[payload.message_id]:
if ((bot.user.id != payload.user_id) and (payload.emoji.name == reaction_emoji)):
role = get(payload.member.guild.roles, name=role_list[payload.message_id][roleIndex])
await payload.member.add_roles(role)
roleIndex += 1
@bot.event
async def on_raw_reaction_remove(payload):
roleIndex = 0
for message in active_messages:
if (payload.message_id == message.id):
for reaction_emoji in reaction_list[payload.message_id]:
if ((bot.user.id != payload.user_id) and (payload.emoji.name == reaction_emoji)):
curr_guild = bot.get_guild(payload.guild_id)
member = curr_guild.get_member(payload.user_id)
role = get(member.guild.roles, name=role_list[payload.message_id][roleIndex])
await member.remove_roles(role)
roleIndex += 1
@bot.command()
async def create(ctx, *, json_message):
if (await verify(ctx, "ewb bot")):
try:
json_object = json.loads(json_message)
except:
await warn(ctx, "Incorrect JSON format!")
await warn(ctx, json_message)
return
# json object created
try:
heading = json_object["heading"]
except:
await warn(ctx, "No 'heading' provided!")
return
description = ""
color = 0xABCDEF # did this as joke but it actually look good lol
if "description" in json_object:
description = json_object["description"]
if "color" in json_object:
color = int(json_object["color"], 16)
hasRole = False
hasReaction = False
if "roles" in json_object:
hasRole = True
if "reactions" in json_object:
hasReaction = True
if (hasRole and not hasReaction) or (hasReaction and not hasRole):
await warn(ctx, "Roles and reactions must be equal!")
return
elif (hasRole and hasReaction):
if (len(json_object["roles"]) != len(json_object["reactions"])):
await warn(ctx, "Roles and reactions must be equal!")
return
# check that all roles exist
roleCount = 0
for roleName in json_object["roles"]:
role = get(ctx.message.guild.roles, name = roleName)
if not role:
await warn(ctx, "Role '" + roleName + "' does not exist in this server!")
return
# put roles in description
description = description + "\n" + json_object["reactions"][roleCount] + " = " + roleName
roleCount = roleCount + 1
embed_object = discord.Embed(
title = heading,
description = description,
color = color,
)
ref = await ctx.send(embed = embed_object)
if (ref):
await ctx.message.delete()
# No need to listen if the message isn't meant for reacting
if (hasRole and hasReaction):
active_messages.append(ref)
reaction_emoji_list = []
for reaction_emoji in json_object["reactions"]:
reaction_emoji_list.append(reaction_emoji)
reaction_list[ref.id] = reaction_emoji_list
raw_role_list = []
for role in json_object["roles"]:
raw_role_list.append(role)
role_list[ref.id] = raw_role_list
# adds reactions to message (aka ref)
await attempt_add_reaction(ref, json_object["reactions"], ctx)
bot.run(TOKEN)