【问题标题】:How should I make my discord bot welcome somebody?我应该如何让我的不和谐机器人欢迎某人?
【发布时间】:2021-03-31 11:20:59
【问题描述】:

我正在 discord.py 中创建我的 discord 机器人,并遇到了我一直试图解决的一个问题。我希望我的机器人对加入服务器的人进行自定义欢迎。到目前为止我的代码:

@client.event
async def on_member_join(ctx, member):
    id = client.get_guild(x)
    general = client.get_channel(x)
    
    if discord.on_member_join:
        general.send(f"{member.mention} has joined the server!")

VS 代码在不和谐时给我一个错误:Module 'discord' has no 'on_member_join' memberpylint(no-member)

两个问题:

  1. 如何实现我最初想要的 - 在我已获得 ID 的频道中发送消息?
  2. 为什么它会给我这个错误(为了我更好地理解)?

PS:隐藏客户端 ID 只是为了安全起见,不知道是否重要,但安全总比抱歉好!而且我也是 discord.py 的新手

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    试试这个:

    @client.event  #Event to detect when a user joins
    async def on_member_join(member):
        general = client.get_channel(id)    #Where id is the channel's ID
        await general.send(member.mention + ' has joined the server!')
    

    【讨论】:

    • 嘿,谢谢!我尝试了您的代码,它没有给我任何错误,但是当我运行它并使用我的 alt 加入服务器时,我在终端中收到此错误:忽略 on_member_join Traceback 中的异常(最后一次调用):文件“ C:\Users\saket\AppData\Roaming\Python\Python39\site-packages\discord\client.py",第 333 行,在 _run_event 等待 coro(*args, **kwargs) 文件“c:\Users\saket\ .vscode\PythonBots\bruh_bot.py",第 115 行,在 on_member_join await general.send(member.mention + ' 已加入服务器!') AttributeError: 'NoneType' object has no attribute 'send'
    • 嗨,我自己修好了。貌似id = client.get_guild(x),“x”应该是服务器ID,然后一般是频道ID。其他一切都很完美,老实说,我不知道为什么我自己没有想到这一点。感谢您的帮助:D
    • @hurb,对不起,我犯了一个错误。你根本不需要get_guild()。复制您的代码后我忘记将其删除。我已经编辑了我的答案。
    • 是的,我看到了。谢谢你帮助我
    猜你喜欢
    • 2020-11-18
    • 2020-12-30
    • 2022-01-23
    • 1970-01-01
    • 2020-11-25
    • 2021-04-27
    • 2019-06-08
    • 2021-06-24
    • 2021-02-07
    相关资源
    最近更新 更多