-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
155 lines (131 loc) · 4.95 KB
/
bot.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
# invite link:
# https://discord.com/api/oauth2/authorize?client_id={client_id}&permissions=8&scope=bot
# run on Heroku server and locally
from email import message
import discord
from discord.ext import commands
from discord.utils import get
import os
from dotenv import load_dotenv
from constants import all_roles_dict, all_roles_list, special_roles
load_dotenv()
intents = discord.Intents.all()
client = commands.Bot(command_prefix=commands.when_mentioned_or("$"), intents=intents)
def getToken():
# code to open and read token
return os.environ.get("TOKEN")
@client.event
async def on_ready():
await client.change_presence(
status=discord.Status.online,
activity=discord.Game(
name="React to my messages in #react-roles to show how cracked you are"
),
)
print("My body is ready")
print("We have logged in as {0.user}".format(client))
print("Name: {}".format(client.user.name))
print("ID: {}".format(client.user.id))
@client.event
async def on_message(message):
await client.process_commands(message)
if (message.author.bot) and (
message.author.id == client.user.id
): # checks if message is from bot
print(message.embeds)
for embed in message.embeds:
for field in embed.fields:
value = field.value
for i in range(len(all_roles_list)):
if all_roles_list[i] in value:
await message.add_reaction(all_roles_list[i])
else:
print(message.author.name)
@client.event
async def on_raw_reaction_add(payload):
if payload.user_id != client.user.id: # checks if reaction is from bot
print("Add role initiated")
guild_id = payload.guild_id
guild = discord.utils.find(lambda g: g.id == guild_id, client.guilds)
value = payload.emoji.name
if all_roles_dict[value] is not None:
role = discord.utils.get(guild.roles, name=all_roles_dict[value])
while role is None:
await guild.create_role(name=all_roles_dict[value], mentionable=True)
role = discord.utils.get(guild.roles, name=all_roles_dict[value])
await role.edit(mentionable=True)
print(role)
member = payload.member
if member:
await member.add_roles(role)
print("success")
else:
print("Member not found")
else:
print("Role not found")
else:
print("The bot reacted")
print(payload.user_id)
@client.event
async def on_raw_reaction_remove(payload):
if payload.user_id != client.user.id: # checks if reaction is from bot
print("Add role initiated")
guild_id = payload.guild_id
guild = discord.utils.find(lambda g: g.id == guild_id, client.guilds)
value = payload.emoji.name
if all_roles_dict[value] is not None:
role = discord.utils.get(guild.roles, name=all_roles_dict[value])
while role is None:
await guild.create_role(name=all_roles_dict[value], mentionable=True)
role = discord.utils.get(guild.roles, name=all_roles_dict[value])
await role.edit(mentionable=True)
print(role)
member = get(guild.members, id=payload.user_id)
if member:
await member.remove_roles(role)
print("success")
else:
print("Member not found")
else:
print("Role not found")
else:
print("The bot reacted")
print(payload.user_id)
@client.command()
async def setroles(ctx):
channel = ctx.message.channel
if ctx.message.author.guild_permissions.administrator:
async for message in channel.history(
limit=10000
): # clears all of the bot messages in the channel
if message.author == client.user:
await message.delete()
await ctx.message.delete()
global special_roles
# TODO remove previous messages the bot sent in the channel
for key in special_roles.keys():
embed = discord.Embed(color=0xFF0000)
embed.add_field(
name=f"Role Menu: {key}",
value="React to give yourself a role.",
inline=False,
)
for subkeys in special_roles[key].keys():
embed.add_field(
name="\u200b",
value=f"{subkeys} : {special_roles[key][subkeys]}",
inline=False,
)
await ctx.send(embed=embed)
else:
msg = f"Sorry {ctx.message.author.mention}, only Admins can use this command"
await channel.send(msg)
@client.command()
async def test(ctx, *, arg):
print(arg)
await ctx.send(arg)
@client.command()
async def ping(ctx):
print(ctx)
await ctx.send(f"Pong! {round(client.latency * 1000)} ms")
client.run(getToken())