-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.py
195 lines (152 loc) · 6.62 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
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
193
194
195
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import os
import random
import re
import discord
from discord.ext import commands
from discord_slash import SlashCommand
from discord_slash.utils.manage_commands import create_option
#TestFile
global category
random.seed()
secrets_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'secrets.json')
with open(secrets_file, 'r') as sec:
TOKEN = json.load(sec)["TOKEN"]
vanity_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'vanity_roles.json')
config_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'config.json')
with open(config_file, 'r') as file:
config = json.load(file)
bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())
slash = SlashCommand(bot, sync_commands=True)
@slash.slash(name="gru", guild_ids=[config["server_id"]], description="Assign yourself vanity role.",
options=[
create_option(
name="name",
description="This is the name of the role.",
option_type=3,
required=True
),
create_option(
name="color",
description="This is the color of the role.",
option_type=3,
required=True
)
])
async def gru(ctx, name: str, color: str):
if not re.match(r"^#[a-fA-F0-9]{6}$", color) or re.match(r"^[a-zß]", name):
await ctx.send(
content=f"**Invalid input:** '{name}, {color}'\n"
f"`name` is not allowed to start with a lowercase letter\n"
f"`color` needs to be a six digit hexadecimal number beginning with a '#'.")
return
with open(vanity_file, 'r') as fp:
data = json.load(fp)
guild = ctx.guild
if str(ctx.author.id) in data:
role_id = data[str(ctx.author.id)]
role = guild.get_role(role_id)
await role.edit(name=name, color=discord.Colour(int(color[1:], 16)), reason="Edited vanity role")
await ctx.send(content=f"Edited role of {ctx.author.name} to {name}, {color}")
else:
new_role = await guild.create_role(name=name, colour=discord.Colour(int(color[1:], 16)),
reason='Created vanity role')
user = ctx.author
await user.add_roles(new_role, reason='Added vanity role')
data[str(ctx.author.id)] = new_role.id
with open(vanity_file, 'w') as fp:
json.dump(data, fp, sort_keys=True, indent=4)
await ctx.send(content=f"Added role {name}, {color} to {ctx.author.name}")
@slash.slash(name="ungru", guild_ids=[config["server_id"]], description="Remove vanity role",
options=[
create_option(
name="user",
description="This is the username (Admin only)",
option_type=6,
required=False
),
create_option(
name="reason",
description="This is the reason (Admin only)",
option_type=3,
required=False
)
])
async def ungru(ctx, user=None, reason=None):
if (user or reason) and not ctx.guild.get_role(config["admin_role_id"]) in ctx.author.roles:
await ctx.send(content=f"Hallo {ctx.author.name}. Du Kek hast nicht die Rechte! <:honkler:721352866127675413>")
return
if not user and reason:
await ctx.send(
content=f"Hallo {ctx.author.name}. Du Kek hast den user vergessen! <:honkler:721352866127675413>")
return
if user:
with open(vanity_file, 'r') as fp:
data = json.load(fp)
if str(user.id) not in data:
await ctx.send(content=f"This user does not have a vanity role.")
return
roleid = data[str(user.id)]
del data[str(user.id)]
with open(vanity_file, 'w') as fp:
json.dump(data, fp, sort_keys=True, indent=4)
role = ctx.guild.get_role(roleid)
await role.delete(reason=f"Removed by {ctx.author.name}. Reason: {reason}")
await ctx.send(content=f"Removed {user}'s vanity role: {role}. Reason being: {reason}")
else:
with open(vanity_file, 'r') as fp:
data = json.load(fp)
if str(ctx.author.id) not in data:
await ctx.send(content=f"You do not have a vanity role.")
return
roleid = data[str(ctx.author.id)]
del data[str(ctx.author.id)]
with open(vanity_file, 'w') as fp:
json.dump(data, fp, sort_keys=True, indent=4)
role = ctx.guild.get_role(roleid)
await role.delete(reason=f"Removed by {ctx.author.name}.")
await ctx.send(content=f"Successfully removed your vanity role: {role}")
@bot.event
async def on_ready():
global category
print(f'{bot.user} has connected to Discord!')
guild = next(g for g in bot.guilds if g.id == config["server_id"])
category = next(c for c in guild.categories if c.id == config["dynamic_category_id"])
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name='over the inn'))
@bot.event
async def on_message(message):
for channel in config["moderated_channels"]:
if message.channel.id == channel:
for link in config["whitelisted_previews"]:
if link in message.content:
return
await message.edit(suppress=True)
@bot.event
async def on_voice_state_update(member, before, after):
if before.channel is None:
await connect(after)
return
if after.channel is None:
await disconnect(before)
return
if before.channel != after.channel:
await move(before, after)
async def connect(after):
if len(after.channel.members) == 1 and after.channel.category_id == config["dynamic_category_id"]:
for channel in category.voice_channels:
if len(channel.members) == 0:
return
await category.voice_channels[0].clone(
name=random.choice(config["channel_name_prefix"]) + random.choice(config["channel_name_suffix"]))
async def disconnect(before):
if len([c for c in category.voice_channels if len(c.members) == 0]) == 1:
return
if len(category.voice_channels) > 1 and len(
before.channel.members) == 0 and before.channel.category_id == config["dynamic_category_id"]:
await before.channel.delete()
async def move(before, after):
await connect(after)
await disconnect(before)
bot.run(TOKEN)