-
-
Save peco2282/a37dad4b912e931599d5a60723fd064f to your computer and use it in GitHub Desktop.
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 discord | |
from discord.ext import commands | |
from motor import motor_asyncio as motor | |
allowed_mentions = discord.AllowedMentions(replied_user=False) | |
bot = commands.Bot( | |
command_prefix="p ", | |
intents=discord.Intents.all(), | |
allowed_mentions=allowed_mentions | |
) | |
dbclient = motor.AsyncIOMotorClient( | |
"" | |
) | |
db = dbclient["ProfileBot"] | |
profiles_collection = db.profiles | |
MAN_CHANNEL_ID = 0000000000000000 | |
WOMAN_CHANNEL_ID = 1111111111111111111 | |
# メッセージを発したチャンネルが男性女性と指定されたチャンネルでない場合、無効なチャンネルとして除外される | |
# ユーザーが参加していなかったらボイスチャンネルに参加するよう促される | |
@bot.command(name="vlist") | |
async def show_profile_in_vc_channel( | |
ctx: commands.Context | |
# channel: discord.VoiceChannel = None | |
): | |
man_text = bot.get_channel(MAN_CHANNEL_ID) | |
woman_text = bot.get_channel(WOMAN_CHANNEL_ID) | |
# 指定のチャンネルで入力されていない時 | |
if ctx.channel not in (man_text, woman_text): | |
return await ctx.send("無効なチャンネルです") | |
# ボイス状態の取得 | |
state: discord.VoiceState = ctx.author.voice | |
# チャンネルに参加していない時 | |
if state is None: | |
return await ctx.send("ボイスチャンネルに参加してください。") | |
# メンバーのいるボイスチャンネルを探す | |
channel = state.channel | |
# 指定されてボイスチャンネルのメンバーのリストが返ります。 | |
vc_members = channel.members | |
# ------------------ 以下変更なし ----------------- | |
# 登録されているすべてのユーザー情報が返ります。 | |
result = profiles_collection.find() | |
_list = await result.to_list(None) | |
_match = [] | |
for member in vc_members: | |
for data in _list: | |
# もしボイスチャンネルのメンバーと登録されているユーザーIDがあれば、 | |
if member.id == int(data["userid"]): | |
# _matchリストにメンション、表示するテキストの入ったタプルが入ります。 | |
# また、Dict.get()では、第2引数でキーがなかった時のデフォルトを設定することができます。 | |
_match.append((member.mention, data.get("text", ""))) | |
description = "\n".join(f"{m[0]}: {m[1]}" for m in _match) or "該当なし" | |
embed = discord.Embed(title="ユーザー情報", description=description) | |
await ctx.send(embed=embed) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment