【问题标题】:Is there anything to make on_member_join in discord.py work?有什么可以让 discord.py 中的 on_member_join 起作用吗?
【发布时间】:2020-12-18 01:53:59
【问题描述】:

我目前正在编写一个机器人,我的第一个目标是让机器人受到欢迎并与加入或离开的成员说再见,但机器人什么也没发送,甚至没有显示任何错误。

@client.event
async def on_member_join(member):
    print(" System: A member has joined the server!")
    def sprint(str):
        for c in str + '\n':
            sys.stdout.write(c)
            sys.stdout.flush()
            time.sleep(3./90)
    sprint(" Bot: Oh hey, looks like "+member.name+" has joined..")
    
    #send message to a specific channel
    
    channel = client.get_channel(channel id from the welcome channel)
    await channel.send("Hey! Welcome to the server, "+member.mention+"! I hope you read the #rules before doing anything, otherwise have fun and enjoy your stay!")

我上次在重新创建脚本之前就这样做了,但是 on_member_join 的东西不起作用,甚至 on_member_leave 也不起作用! (我首先在做on_member_join 以检查它是否正常工作)

我的备用帐户重新加入了我的服务器,但机器人(MEE6 除外)没有发送任何内容。你们有什么可以帮助我的吗?

【问题讨论】:

  • 您是否启用了intents.members
  • @ŁukaszKwieciński 我在脚本中的任何地方都没有看到intents.members。我在哪里添加它?这是我的代码的开头:import os import discord import discord.ext from discord.ext import commands TOKEN = 'token from my bot' intents = discord.Intents(members=True) client = discord.Client(intents=intents) bot = commands.Bot(command_prefix='?') @client.event async def on_ready(): print(" System: Bot is ready!")

标签: discord.py


【解决方案1】:

您需要通过创建discord.Intents.default 的实例、将 Intents.members 设置为 True 并将实例传递给您的 commands.Bot 或 discord.Client 构造函数来启用成员意图,例如:

intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(intents=intents)

您还需要在 Discord 开发门户 (https://discord.com/developers) 中启用成员意图。转到该 URL,单击您的应用程序,转到 Bot 选项卡,向下滚动到“特权网关意图”部分,然后启用成员意图。 然后,重新启动您的机器人,成员事件应该可以工作。 (更多信息,请查看https://discordpy.readthedocs.io/en/latest/intents.html

【讨论】:

  • 它仍然无法正常工作...我已经启用了“服务器成员意图”,但机器人仍然没有发送任何内容..
  • 由于您同时拥有 discord.Client 和 commands.Bot 的实例(您不应该这样做),并且您只为您的 Client 实例注册了事件,因此请确保将意图传递给client,不是bot
  • 我已经将意图传递给client...
  • 代码的开头是什么样的? (您在其中定义意图和您的客户端实例)
  • import os import discord import discord.ext TOKEN = 'token from my bot' intents = discord.Intents.default() intents.members = True client = discord.Client(intents=intents) @client.event async def on_ready(): print(" System: Bot is ready!")
猜你喜欢
  • 2021-02-25
  • 2020-09-19
  • 2021-07-15
  • 2021-01-16
  • 2021-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多