|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 |
|
3 | 3 | import discord |
| 4 | +from discord import embeds |
4 | 5 | from discord.ext import tasks, commands |
5 | 6 | import logging |
6 | 7 | import os |
|
14 | 15 | logging.getLogger("discord.client").setLevel(logging.ERROR) |
15 | 16 | logger = logging.getLogger(__name__) |
16 | 17 |
|
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) |
18 | 25 |
|
19 | 26 | known_events = load_known_events() |
20 | 27 |
|
| 28 | +emoji = "🚩" |
21 | 29 |
|
22 | 30 | @bot.event |
23 | 31 | async def on_ready(): |
@@ -108,12 +116,48 @@ async def create_CTF_channel(ctx, channel_name:str): |
108 | 116 | await ctx.send(f"Category '{category_name}' not found.") |
109 | 117 | return |
110 | 118 |
|
| 119 | + overwrites = { |
| 120 | + guild.default_role: discord.PermissionOverwrite(view_channel=False), |
| 121 | + ctx.author: discord.PermissionOverwrite(view_channel=True) |
| 122 | + } |
| 123 | + |
111 | 124 | 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 |
114 | 142 | except Exception as e: |
115 | 143 | await ctx.send(f"Failed to create channel: {e}") |
116 | 144 |
|
| 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}") |
117 | 161 |
|
118 | 162 | if __name__ == "__main__": |
119 | 163 | main() |
0 commit comments