在用户加入事件上发送消息-discord.py [英] Sending a message on a user join event - discord.py

查看:46
本文介绍了在用户加入事件上发送消息-discord.py的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在编程一个机器人,当用户加入时该机器人会欢迎用户,但它似乎无法正常工作.

I've been programming a bot that welcomes a user when they join but it doesn't seem to be working.

async def on_member_join(member):
    channel = member.channel
    await channel.send(f'{member} has arrived')

推荐答案

您尚未指定要向其发送消息的频道其中,以及 discord.Member 没有名为 channel 的属性.

You haven't specified where the channel is that you want to send the message to, and discord.Member doesn't have an attribute called channel.

您必须通过其ID获取频道,就像这样:

You'll have to get the channel via its ID, like so:

async def on_member_join(member):
    channel = bot.get_channel(112233445566778899) # replace id with the welcome channel's id
    await channel.send(f"{member} has arrived!")

如果您愿意,也可以通过其名称获取它:

If you want to, you can also get it via its name:

async def on_member_join(member):
    channel = discord.utils.get(member.guild.text_channels, name="welcome")
    await channel.send(f"{member} has arrived!")


参考:

这篇关于在用户加入事件上发送消息-discord.py的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆