当用户到达时,Discord 机器人不会向频道发送消息



我有问题的机器人不发送消息到一个特定的频道与新用户的到来,我尝试了很多事情。下面是代码:

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函数不向指定通道发送消息的原因是什么?

也许你没有听说过网关意图。很"新"。

在你的情况下,你想跟踪成员,当他们加入一个公会,所以你需要启用Members特权意图能够跟踪它。

下面是一个关于如何实现它的小示例

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的应用页面上为你的bot启用了成员特权意图。正如你在这个例子中看到的。

如果你需要进一步的帮助,这里有几个链接:

<<ul>
  • discord.py文档/gh><<li>不和文档/gh>祝你编程愉快^^

  • 这应该可以工作,因为它被称为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
    

    最新更新