Created
August 4, 2020 13:02
-
-
Save YuzuRyo61/fca8350016fd0df7688bac7234989306 to your computer and use it in GitHub Desktop.
Zoomの待機室のようなやつをDiscordのBotで再現してみたかった(お遊びスクリプト。Discord.pyとtoml使用)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[wating_room] | |
wating_room_id = 741234567894561232 # 待機室のボイスチャンネルのID | |
main_room_id = 741234567894561232 # メインのボイスチャンネルのID | |
text_channel_id = 741234567894561232 # メインのテキストチャンネルのID | |
role_id = 7400000000000000 # 参加承認時に付与されるロールのID |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import toml | |
import logging | |
import discord | |
from discord.ext import commands | |
with open("config.toml", mode="r") as f: | |
CONFIG = toml.load(f) | |
class WatingRoom(commands.Cog): | |
def __init__(self, bot): | |
self.bot = bot | |
@commands.Cog.listener() | |
async def on_ready(self): | |
self.text_channel = self.bot.get_channel(CONFIG["wating_room"]["text_channel_id"]) | |
self.main_room = self.bot.get_channel(CONFIG["wating_room"]["main_room_id"]) | |
self.wating_room = self.bot.get_channel(CONFIG["wating_room"]["wating_room_id"]) | |
self.role_id = CONFIG["wating_room"]["role_id"] | |
if self.text_channel is None or \ | |
not isinstance(self.text_channel, discord.TextChannel): | |
logging.error("Unknown channel") | |
self.bot.remove_cog("WatingRoom") | |
@commands.Cog.listener() | |
async def on_voice_state_update(self, member, before, after): | |
# Botは無視 | |
if member.bot: | |
return | |
# チャンネルが同じ場合は無視 | |
if before.channel is after.channel: | |
return | |
# 離脱した場合はロール削除 | |
if after.channel is None: | |
await member.remove_roles( | |
member.guild.get_role(self.role_id), | |
reason="ユーザーが退出" | |
) | |
return | |
if after.channel is self.wating_room: | |
embed = discord.Embed( | |
title="待機室にユーザーがいます", | |
description=( | |
f"{str(member)}が{str(self.main_room)}に参加したいようです。\n" | |
"許可する場合は✅、拒否する場合は❎のリアクションを送って下さい。" | |
) | |
) | |
waiting_msg = await self.text_channel.send(embed=embed) | |
await waiting_msg.add_reaction("✅") | |
await waiting_msg.add_reaction("❎") | |
def decide_join(reaction, user): | |
return not user.bot and reaction.message.id == waiting_msg.id and str(reaction.emoji) in ["✅", "❎"] | |
reaction, _ = await self.bot.wait_for( | |
"reaction_add", | |
check=decide_join | |
) | |
await waiting_msg.delete() | |
if str(reaction.emoji) == "✅": | |
await member.add_roles( | |
member.guild.get_role(self.role_id) | |
) | |
await member.move_to(self.main_room) | |
elif str(reaction.emoji) == "❎": | |
await member.move_to(None) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment