python - 收到确认后添加角色
问题描述
一旦有人加入并从同一频道中的某个人那里得到“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))
解决方案
一种方法是使用该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
推荐阅读
- python - 多处理值挂起并锁定
- c - 从机器的角度理解汇编程序中的指针
- javascript - Expressjs 在 6 点后停止处理请求,然后在经过大量时间后恢复
- php - 如何在 localhost 上触发 Laravel SQLSTATE 错误?
- php - 如何自动填充现有分类术语的描述?
- python - 在 Jupyter 笔记本中出现以下错误。不确定问题是否与 collect() 或任何兼容性问题有关
- python-3.x - (psycopg2.OperationalError) FATAL: 数据库系统正在启动,docker + redis + celery
- javascript - 如何使用javascript查询数据库并将值放入数组
- postgresql - 带有 rds.force_ssl 的 AWS RDS Postgres 上的 Debezium 无法正常工作
- scala - 跳过无限流中的错误