【问题标题】:Discord bot won't send a message to the channel when a user arrives [duplicate]当用户到达时,Discord bot 不会向频道发送消息[重复]
【发布时间】:2021-05-15 01:41:22
【问题描述】:

我遇到了机器人没有在新用户到来时向特定频道发送消息的问题,我尝试了很多方法。代码如下:

import discord

from discord.ext import commands

TOKEN = "Random token string"
PREFIX = "!"

client = commands.Bot(command_prefix = PREFIX, case_insensitive = True)

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.event
async def on_member_join(member):
   print("it should work")
   await client.get_channel(Int channel ID).send(f"{member.name} has joined")

client.run ("Some random token")

我没有收到任何错误,但是 on_member_join 函数没有向指定频道发送消息的原因是什么?

【问题讨论】:

  • 您是否通过开发者门户启用了成员意图并在您的代码中进行了定义?即使您可能只是向频道发送消息,会员加入活动也需要这样做
  • 是的,我启用了机器人的所有设置,基本上给了它完整的管理模式,但显然它与下面的评论者给我的 sn-p 有关,它只是神奇地工作。跨度>

标签: python discord discord.py bots


【解决方案1】:

也许您还没有听说过网关意图。这很“新”。

在您的情况下,您希望在成员加入公会时对其进行跟踪,因此您需要启用 Members Privileged Intent 才能对其进行跟踪。

这是一个关于如何实现的小例子

intents = discord.Intents().default()
intents.members = True # Enable the member Privileged Intent

# Add the intents to the bot object
client = commands.Bot(command_prefix = PREFIX, case_insensitive = True, intents=intents)

确保您已在 Discord 的 application page 上为您的机器人启用成员特权意图。 正如您在example 中看到的那样。

如果您需要更多帮助,请点击以下链接:

祝你有美好的一天,快乐的编码^^

【讨论】:

  • 我尝试了类似的方法来添加意图,但没有奏效,但出于某种奇怪的原因,我添加了您的 sn-p,经过所有这些小时的尝试,它终于奏效了!非常感谢,谢谢!
【解决方案2】:

这应该可以工作,它被称为intents 而不是case_insensitive

from discord import Intents
from discord import Game, Intents
from discord.utils import get
import discord
from discord.ext import commands



@client.event
async def on_member_join(member):
    if member.guild.name == 'server name': #make sure it is right with caps and spaces
        embed = discord.Embed(title=f'welcome {member.mention} !\nwelcome to {member.guild.name}',
                    color=0x0061ff)
        embed.set_thumbnail(url=ctx.author.avatar_url)
        await client.get_channel(channel id).send(embed=embed) #make sure the channel id is correct
#you can add more of those
    else:
        return

【讨论】:

  • 您还应该在您的不和谐开发者门户设置中启用意图。
猜你喜欢
  • 2021-01-10
  • 2023-03-07
  • 2022-01-05
  • 2021-03-24
  • 2021-08-18
  • 1970-01-01
  • 2020-04-07
  • 2023-03-27
  • 2020-07-31
相关资源
最近更新 更多