-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
244 lines (213 loc) · 10.8 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
#!/usr/bin/env python3
import json
import asyncio
from asyncio import create_task, Task, wait, FIRST_COMPLETED
from typing import Optional, Tuple, List
import nextcord
from nextcord.ext.commands import Bot, Context, command, has_permissions
from nextcord.ext import application_checks
from nextcord import SlashOption, User, Member, Role, application_command, Embed, Attachment
from nextcord.abc import GuildChannel, Messageable
from nextcord.activity import Activity, ActivityType
from nextcord.interactions import Interaction
from nextcord.message import Message
from nextcord.channel import TextChannel
from nextcord.threads import Thread
from nextcord.embeds import Embed
from nextcord.colour import Color
from nextcord.utils import get
from nextcord.ext.commands.errors import MissingPermissions
from nextcord.errors import Forbidden, HTTPException
CATEGORY_TEAMS = 1033500272372228277
OIS = Bot(
command_prefix=("OIS)"),
strip_after_prefix=True,
description="OIS Bot for the OIS Discord Server",
owner_id=797844636281995274,
activity=Activity(type=ActivityType.competing, name="Olimpiadi Informatiche a Squadre"),
status=nextcord.Status.online,
intents=nextcord.Intents.all(),
)
@OIS.event
async def on_ready() -> None:
print(f"{OIS.user.name}#{OIS.user.discriminator} is online")
@OIS.slash_command(name="delete", description="Delete the last N messages")
async def delete(
interaction: Interaction,
number: int = SlashOption(
name="number",
description="The number of messages to delete",
required=True,
)) -> None:
if not interaction.user.guild_permissions.administrator:
await interaction.response.send_message("You must be the owner of the server to use this command",
ephemeral=True)
return
await interaction.channel.purge(limit=number + 1)
await interaction.response.send_message(f"Deleted {number} messages", ephemeral=True)
@OIS.slash_command(name="create_team", description="Create your team")
async def create_team(
interaction: Interaction,
team_name: str = SlashOption(
name="team_name",
description="The name of your team",
required=True,
),
team_city: str = SlashOption(
name="team_city",
description="The city of your team",
required=True,
)) -> None:
team_leader = interaction.user
embed = Embed(title="Team Creation", description="Send :white_check_mark: if you are sure of the following.",
color=Color.green())
embed.add_field(name="Team Name", value=team_name, inline=False)
embed.add_field(name="Team City", value=team_city, inline=False)
embed.add_field(name="Team Leader", value=team_leader.mention, inline=False)
embed.set_footer(text="Send :white_check_mark: quickly to confirm the creation of the team")
await interaction.response.send_message(embed=embed, ephemeral=False)
if (role := get(interaction.guild.roles, name=team_name)) is not None:
await interaction.channel.send("A team with this name already exists")
else:
role: Role = await interaction.guild.create_role(
name=team_name,
color=Color.random(),
hoist=True
)
await interaction.channel.send("Role created successfully")
# Assign the role to the team leader
await team_leader.add_roles(role)
await interaction.channel.send("Role assigned successfully")
# Create channel (https://stackoverflow.com/questions/68235517/creating-a-channel-in-specific-category-the-category-id-is-should-be-variable)
category = get(interaction.guild.categories, id=CATEGORY_TEAMS)
channel: GuildChannel = await interaction.guild.create_text_channel(
name=team_name,
category=category,
topic=f"{team_name} - {team_city}",
reason=f"Team {team_name} creation"
)
await interaction.channel.send("Channel created successfully")
# Manage permissions of the channel for @everyone
channel_permissions = channel.permissions_for(interaction.guild.default_role)
channel_permissions.send_messages = False
channel_permissions.add_reactions = True
# Manage permissions of the channel for the team members
channel_permissions = channel.permissions_for(role)
channel_permissions = nextcord.Permissions.all()
@OIS.slash_command(name="embed", description="Embed message given its url")
async def embed(interaction: Interaction, message_link: str) -> None:
message_metadata = message_link.split("/")
message_channel = interaction.guild.get_channel(int(message_metadata[5]))
message = await message_channel.fetch_message(int(message_metadata[6]))
embedded_response = make_embedded_message(message)
if len(embedded_response) == 1:
await interaction.response.send_message(embed=embedded_response[0])
else:
await interaction.response.send_message(embeds=embedded_response)
@OIS.slash_command(name="hall_of_fame", description="Create the Hall of Fame for a channel from its pinned messages")
async def hall_of_fame(interaction: Interaction, source_channel: TextChannel) -> None:
target_channel: Thread = await source_channel.create_thread(
name=f"Hall of Fame",
reason="Hall of Fame",
type=nextcord.ChannelType.public_thread
)
await interaction.channel.send(f"Creating Hall of Fame for {source_channel.mention} in {target_channel.mention}...")
await target_channel.send(f"**Hall of Fame for {source_channel.mention}!**")
pinned = await source_channel.pins()
for message in reversed(pinned):
try:
embedded_response = make_embedded_message(message)
except HTTPException:
await target_channel.send("<Error-processing-message>")
if len(embedded_response) == 1:
try:
await target_channel.send(embed=embedded_response[0])
except HTTPException:
await target_channel.send("<Error-processing-message>")
else:
try:
await target_channel.send(embeds=embedded_response)
except nextcord.errors.HTTPException as e:
if (e.code == 50035):
print("embeds >= 10; skipping embeds")
await target_channel.send(embed=embedded_response[0])
else:
raise e
await interaction.channel.send(f"Hall of Fame for {source_channel.mention} created in {target_channel.mention}!")
@OIS.slash_command(name="unpin_all", description="Unpin all pinned messages of a channel")
@application_checks.has_permissions(administrator=True)
async def unpin_all(interaction: Interaction,
target_channel: Optional[TextChannel] = SlashOption(required=False)) -> None:
target_channel = target_channel or interaction.channel
pinned = await target_channel.pins()
for pin in pinned:
await pin.unpin()
await interaction.response.send_message(f"Unpinned all pins in {target_channel.mention}")
@OIS.slash_command(name="reaction_role", description="Create a reaction role message")
@application_checks.has_permissions(administrator=True)
async def reaction_role(interaction: Interaction, emoji: str, role: Role, timeout: int=None) -> None:
embed = Embed(
title="Reaction Role",
description=f"React with {emoji} to get the {role.mention} role",
color=Color.green()
)
message = await interaction.channel.send(embed=embed)
await message.add_reaction(emoji)
asyncio.create_task(reaction_role_listener(message, emoji, role, timeout))
@OIS.slash_command(name="count_pinned", description="Count the number of pinned messages in a channel by a user")
async def count_pinned(interaction: Interaction, user: Member, channel: Optional[TextChannel] = SlashOption(required=False)) -> None:
pinned: List[Message]
if channel is None:
pinned_in_channel: dict[TextChannel, List[Message]] = {}
for channel in interaction.guild.text_channels:
pinned_in_channel[channel] = await channel.pins()
pinned = [message for channel in pinned_in_channel.values() for message in channel if message.author == user]
embed_ = Embed(title=f"Pinned messages by {user.display_name}")
for channel, messages in pinned_in_channel.items():
pinned_ = [message for message in messages if message.author == user]
if len(pinned_) > 0:
embed_.add_field(name=channel.name, value=f"{len(pinned_)} pinned messages")
embed_.description = f"{user.mention} has {len(pinned)} pinned messages in {interaction.guild.name}"
await interaction.channel.send(embed=embed_)
else:
pinned = await channel.pins()
pinned = [message for message in pinned if message.author == user]
embed_ = Embed(title=f"Pinned messages by {user.display_name}")
embed_.description = f"{user.mention} has {len(pinned)} pinned messages in {channel.mention}"
await interaction.channel.send(embed=embed_)
await interaction.response.send_message("Done", ephemeral=True)
async def reaction_role_listener(message: Message, emoji: str, role: Role, timeout_seconds: int=None) -> None:
while True:
try:
reaction: nextcord.Reaction
user: nextcord.Member
if timeout_seconds is None:
reaction, user = await OIS.wait_for("reaction_add")
else:
reaction, user = await OIS.wait_for("reaction_add", timeout=timeout_seconds)
except TimeoutError:
await message.remove_reaction(emoji, OIS.user)
return
if reaction.message.id == message.id and str(reaction.emoji) == emoji:
await user.add_roles(role)
# await message.remove_reaction(emoji, user)
def make_embedded_message(message: Message) -> list[Embed]:
embedded_response = Embed(
title=f"Message from {message.channel.mention}",
url=message.jump_url,
description=message.content + f"\n\n[**Jump to message**]({message.jump_url})"
)
embedded_response.set_author(name=message.author, icon_url=message.author.display_avatar)
embedded_response.set_footer(text=f"Message ID: {message.id}")
embedded_response = [embedded_response]
if len(message.attachments) == 1:
embedded_response[0].set_image(url=message.attachments[0])
elif len(message.attachments) > 1:
for attachment in message.attachments:
if attachment.content_type.startswith("video"):
continue
new_embed = Embed(url=message.jump_url) # We need all embeds to have the same url for this to work
new_embed.set_image(url=attachment)
embedded_response.append(new_embed)
return embedded_response
OIS.run(open("token.txt").read().strip())