-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
285 lines (232 loc) · 9.09 KB
/
main.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# We need to install pip dependency via the code
# Replit seems to uninstall them after 24 hours
import os
os.system("pip install flagpy discord-components")
import discord
import flagpy as fp
import random
from discord_components import DiscordComponents, Button, ButtonStyle
from discord.ext import commands
from replit import db
from keep_alive import keep_alive
### VARIABLES ###
CATEGORY_ID = int(os.environ['CATEGORY_ID'])
CREATE_CHANNEL_ID = int(os.environ['CHANNEL_ID'])
CREATE_PRIVATE_CHANNEL_ID = int(os.environ['PRIVATE_CHANNEL_ID'])
VOICE_CHANNEL_NAME = '{} cabin'
TEXT_CHANNEL_NAME = '{} logbook'
### RUNNER ###
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix='!', intents=intents)
### CHANNEL HANDLING ###
# HELPERS
ALLOWED_PERMISSIONS = {
"read_messages": True,
"send_messages": True,
"connect": True,
"speak": True,
"view_channel": True,
"manage_permissions": True
}
RESTRICTED_PERMISSIONS = {
"read_messages": False,
"send_messages": False,
"connect": False,
"speak": False,
"view_channel": False,
"manage_permissions": False
}
async def create_channel(member, position, private=False):
voice_channel_name = VOICE_CHANNEL_NAME.format(member.name)
text_channel_name = TEXT_CHANNEL_NAME.format(member.name)
category = client.get_channel(CATEGORY_ID)
overwrites = None if not private else {
member: discord.PermissionOverwrite(**ALLOWED_PERMISSIONS),
member.guild.default_role: discord.PermissionOverwrite(**RESTRICTED_PERMISSIONS)
}
# Todo: Update the video default mode when discord update his API
voice_channel = await member.guild.create_voice_channel(
voice_channel_name,
category=category,
overwrites=overwrites,
bitrate=96000)
text_channel = await member.guild.create_text_channel(
text_channel_name,
category=category,
overwrites=overwrites)
# Link the voice and the text channel in the DB
db[str(voice_channel.id)] = text_channel.id
# Automatically move the channel creator
await member.move_to(voice_channel)
async def delete_channel(voice_channel):
id = str(voice_channel.id)
text_channel = discord.utils.get(voice_channel.guild.channels, id=db[id])
await text_channel.delete()
await voice_channel.delete()
del db[id]
@client.event
async def on_voice_state_update(member, before, after):
if before.channel:
id = str(before.channel.id)
if id in db and not before.channel.members:
await delete_channel(before.channel)
if after.channel:
if after.channel.id == CREATE_CHANNEL_ID:
await create_channel(member, after.channel.category.position)
elif after.channel.id == CREATE_PRIVATE_CHANNEL_ID:
await create_channel(member, after.channel.category.position, private=True)
@client.event
async def on_guild_channel_update(before, after):
text_channels_ids = [
db[key] if type(db[key]) == str else None for key in db.keys()
]
if before.id not in text_channels_ids:
return
voice_channels_ids = [int(key) for key in db.keys()]
index = text_channels_ids.index(before.id)
voice_channel = discord.utils.get(before.guild.channels, id=voice_channels_ids[index])
added_members = [
member for member in after.members if member not in before.members
]
removed_members = [
member for member in before.members if member not in after.members
]
# Handle linked voice channel permissions
for member in removed_members:
await voice_channel.set_permissions(member, **RESTRICTED_PERMISSIONS)
for member in added_members:
await voice_channel.set_permissions(member, **ALLOWED_PERMISSIONS)
await after.set_permissions(member, **ALLOWED_PERMISSIONS)
# Try to move recently added members
try:
await member.move_to(voice_channel)
except Exception:
continue
### RANDOM COMMANDS ###
@client.command(help='You can pass any number of choices to the command !',
brief='Choose a random value among all the given choices.')
async def choice(ctx, *args):
if not args:
await ctx.reply('```You need to give at least one choice !```')
return
choice = random.choice(args)
await ctx.reply(f'```I choose \'{choice}\' ^^ !```')
@client.command(brief='Give the result of a coinflip.')
async def coinflip(ctx):
choice = random.choice(['head', 'tail'])
await ctx.reply(f'```It\'s {choice} !```')
@client.command(help='You must pass two number to the command !',
brief='Choose a random number in a range.')
async def rnd(ctx, min, max):
if not min or not max:
await ctx.reply('```You need to give two args fucker !```')
try:
min = int(min)
max = int(max)
except Exception:
await ctx.reply('```You must give two numbers !```')
if min >= max:
await ctx.reply('```First number must be lower that the second !```')
else:
choice = random.randint(min, max)
await ctx.reply(f'```You got \'{choice}\' !```')
### FLAGS GAME ###
# HELPERS
COMPONENTS = [[Button(label="Reroll"),
Button(label="Verify")],
Button(style=ButtonStyle.red, label="Done")]
COMPONENTS_VERIFY = [[
Button(label="Reroll"),
Button(label="Verify", disabled=True),
Button(style=ButtonStyle.green, label="True"),
Button(style=ButtonStyle.blue, label="False")],
Button(style=ButtonStyle.red, label="Done")]
COMPONENTS_LAST = [[
Button(style=ButtonStyle.red, label="Done"),
Button(label="Verify")
]]
COMPONENTS_LAST_VERIFY = [[
Button(style=ButtonStyle.red, label="Done"),
Button(label="Verify", disabled=True),
Button(style=ButtonStyle.green, label="True"),
Button(style=ButtonStyle.blue, label="False")
]]
# Send Flag message
async def get_random_flag(channel):
# Flags random selection
values = db[str(channel.id)]
country = random.choice(values['LIST'])
fp.get_flag_img(country).save('tmp.png', format='PNG')
values['LIST'].remove(country)
# Response handling
size = len(values['LIST']) + 1
NB_COUNTRY = values['NB_COUNTRY']
content = f'```What is this flag ? ({size} / {NB_COUNTRY})```'
components = COMPONENTS if size > 1 else COMPONENTS_LAST
components[0][1].custom_id = country
await channel.send(content=content, file=discord.File('tmp.png'), components=components)
# Button handling
@client.event
async def on_button_click(interaction):
id = str(interaction.channel.id)
values = db[id] if id in db else None
components = COMPONENTS_VERIFY if values and values['LIST'] else COMPONENTS_LAST_VERIFY
if interaction.component.label == 'Verify':
content = f'```The Answer is \'{interaction.component.id}\' !```'
components[0][2].disabled = True
components[0][3].disabled = False
values['NB_POINT'] += 1
await interaction.edit_origin(content=content, components=components)
elif interaction.component.label == 'True':
if components[0][3].disabled:
values['NB_POINT'] += 1
components[0][2].disabled = True
components[0][3].disabled = False
await interaction.edit_origin(content=interaction.message.content, components=components)
elif interaction.component.label == 'False':
if components[0][2].disabled:
values['NB_POINT'] -= 1
components[0][2].disabled = False
components[0][3].disabled = True
await interaction.edit_origin(content=interaction.message.content, components=components)
elif interaction.component.label == 'Replay':
await interaction.message.delete()
await flags(interaction)
elif interaction.component.label == 'Done' or not values['LIST']:
await interaction.message.delete()
NB_POINT = values['NB_POINT']
content = f'```Congrats, you got {NB_POINT} flags right !```'
await interaction.channel.send(
content=content,
components=[Button(label="Replay", style=ButtonStyle.blue)])
del db[id]
elif interaction.component.label == 'Reroll':
await interaction.message.delete()
if values['LIST']:
await get_random_flag(interaction.channel)
# Command
@client.command(
help='Display a flag, you must guess which one it is ! (Maybe with a score system one day)',
brief='Play a game of \'Guess the flag\'.')
async def flags(ctx):
id = str(ctx.channel.id)
if id in db:
await ctx.reply(content='```You can\'t start another game in this channel !```')
return
country_list = fp.get_country_list()
db[id] = {
'NB_POINT': 0,
'LIST': country_list,
'NB_COUNTRY': len(country_list)
}
await get_random_flag(ctx.channel)
### BOT CONNECTICITY CONTENT ###
@client.event
async def on_ready():
DiscordComponents(client)
await client.change_presence(activity=discord.Game(name=f"!help"))
print("Bot has successfully logged in as: {}".format(client.user))
print("Bot ID: {}".format(client.user.id))
keep_alive()
client.run(os.environ['TOKEN'])