Skip to content

Instantly share code, notes, and snippets.

@peco2282
Created March 8, 2023 09:36
Show Gist options
  • Save peco2282/6f029d574c477e720660437eb8a81bc9 to your computer and use it in GitHub Desktop.
Save peco2282/6f029d574c477e720660437eb8a81bc9 to your computer and use it in GitHub Desktop.
import discord
import motor.motor_asyncio as motor
from discord.ext import commands
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 = ...
WOMAN_CHANNEL_ID = ...
# メンバーがVCにいるチャンネルのユーザーの情報
@bot.command("vlist")
async def show_profile_in_vc_channel(ctx: commands.Context):
if ctx.author.voice is None:
return await ctx.send("VCに参加していません")
# 以下の処理はメンバーがVCに参加時に行われる。
vc = ctx.author.voice.channel
vc_members = vc.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)
@bot.command("set")
async def set_profile(ctx: commands.Context, *, profile: str = None):
man = bot.get_channel(MAN_CHANNEL_ID)
woman = bot.get_channel(WOMAN_CHANNEL_ID)
if ctx.channel.id not in (MAN_CHANNEL_ID, WOMAN_CHANNEL_ID):
return await ctx.send(f"無効なチャンネルです。\n"
f"男性: {man.mention}\n"
f"女性: {woman.mention}")
if profile is None:
return await ctx.send("プロフィールを書いてください。")
new_data = {
"userid": ctx.author.id,
"text": str(profile)
}
result = await profiles_collection.replace_one(
{
"userid": ctx.author.id
},
new_data
)
if result.matched_count == 0:
await profiles_collection.insert_one(new_data)
await ctx.reply("設定が完了したロト~。")
# メッセージのメンション先の人の情報を投稿
@bot.command("show")
async def show_profile(
ctx: commands.Context,
target: discord.User = None
):
if target is None:
target = ctx.author
if not isinstance(target, (discord.User, discord.Member)):
return await ctx.send("メンションしてください。")
profile = await profiles_collection.find_one(
{
"userid": target.id
}, {
"_id": False
}
)
if profile is None:
return await ctx.reply("みつからなかったロト~")
embed = discord.Embed(title=f"`{target.name}`さんのプロフィール", description=profile["text"])
return await ctx.reply(embed=embed)
# 自分の情報を消す。
# メンションしたユーザー情報も消す場合はコメントアウト部分をアンコメントしてください。
@bot.command(name="delete", aliases=["del"])
async def delete_profile(
ctx: commands.Context,
# target: discord.User = None
):
# if target is None:
# target = ctx.author
# if not isinstance(target, discord.User):
# return await ctx.send("メンションしてください。")
result = await profiles_collection.delete_one({
"userid": ctx.author.id
})
if result.deleted_count == 0:
return await ctx.reply("みつからなかったロト~")
return await ctx.reply("けしたロト~")
bot.run("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment