Skip to content

Commit df1833c

Browse files
authored
Merge pull request #14 from LemonTeatw1/master
feat: enhance CTF channel creation with permissions and reaction hand…
2 parents afe24ab + 190f9bf commit df1833c

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

ctfeed.py

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22

33
import discord
4+
from discord import embeds
45
from discord.ext import tasks, commands
56
import logging
67
import os
@@ -14,10 +15,17 @@
1415
logging.getLogger("discord.client").setLevel(logging.ERROR)
1516
logger = logging.getLogger(__name__)
1617

17-
bot = commands.Bot(intents=discord.Intents.default())
18+
intents = discord.Intents.default()
19+
intents.members = True
20+
intents.guilds = True
21+
intents.reactions = True
22+
intents.message_content = True
23+
24+
bot = commands.Bot(intents=intents)
1825

1926
known_events = load_known_events()
2027

28+
emoji = "🚩"
2129

2230
@bot.event
2331
async def on_ready():
@@ -108,12 +116,48 @@ async def create_CTF_channel(ctx, channel_name:str):
108116
await ctx.send(f"Category '{category_name}' not found.")
109117
return
110118

119+
overwrites = {
120+
guild.default_role: discord.PermissionOverwrite(view_channel=False),
121+
ctx.author: discord.PermissionOverwrite(view_channel=True)
122+
}
123+
111124
try:
112-
new_channel = await guild.create_text_channel(channel_name, category=category)
113-
await ctx.send(f"Channel '{new_channel.name}' created in category '{category_name}'.")
125+
new_channel = await guild.create_text_channel(channel_name, category=category, overwrites=overwrites)
126+
msg = await new_channel.send(
127+
embed = discord.Embed(
128+
title=f"{ctx.author.display_name} 發起了 {channel_name}!",
129+
)
130+
)
131+
132+
public_msg = await ctx.send(
133+
embed = discord.Embed(
134+
title=f"新CTF頻道創建{channel_name}",
135+
description=f"加入請按下面的Emoji{emoji}"
136+
)
137+
)
138+
139+
await public_msg.add_reaction(emoji)
140+
bot.ctf_join_message_id = public_msg.id
141+
bot.ctf_channel = new_channel
114142
except Exception as e:
115143
await ctx.send(f"Failed to create channel: {e}")
116144

145+
@bot.event
146+
async def on_raw_reaction_add(payload):
147+
if payload.message_id != getattr(bot, "ctf_join_message_id", None):
148+
return
149+
150+
if str(payload.emoji) != emoji:
151+
return
152+
153+
guild = bot.get_guild(payload.guild_id)
154+
member = guild.get_member(payload.user_id)
155+
if member.bot:
156+
return
157+
158+
channel = bot.ctf_channel
159+
await channel.set_permissions(member, view_channel=True)
160+
print(f"{member} 已加入 {channel.name}")
117161

118162
if __name__ == "__main__":
119163
main()

0 commit comments

Comments
 (0)