首页 > 解决方案 > 收到确认后添加角色

问题描述

一旦有人加入并从同一频道中的某个人那里得到“y”确认,就尝试添加角色

不确定我是否可以在中间添加另一个异步...

抱歉,如果这是一个非常明显的问题,但我在网上得到的大多数答案都已经过时了,而且文档没有回答我所有的问题

这是我的尝试

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.members = True
bot = discord.ext.commands.Bot(command_prefix="$", intents=intents)

@bot.event
async def on_member_join(member):
 await channel.send("{} has just joined. \nDo you know them (y/n)?".format(member.name))
 print("someone joined")
  async def on_message(message):
   if "y" in message.content:
    role = get(message.guild.roles, name="Hey i know you")
    await member.add_roles(role, atomic=True)
    await message.channel.send("added Hey i know you to {}".format(member.name))
       
 

标签: pythondiscord.py

解决方案


一种方法是使用该wait_for函数。

@client.event
async def on_member_join(member):
    channel = client.get_channel(your channel id)
    guild = client.get_guild(your guild id)
    await channel.send(f"{member.mention} joined!")
    await asyncio.sleep(3)
    await channel.send("{} has just joined. \nDo you know them (y/n)?".format(member.name))
    msg = await client.wait_for("message")
    if msg.content == "y":
        role = guild.get_role(your rold id)
        await member.add_roles(role)
        await channel.send("Added role!")
    elif msg.content == "n":
        await channel.send("i wont add the role!")
        return

推荐阅读