我遇到了机器人无法在新用户到来时向特定频道发送消息的问题,我尝试了很多方法。代码如下:
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 的函数没有向指定频道发送消息的原因是什么?
答案 0 :(得分:1)
也许您还没有听说过网关意图。这是相当“新”的。
在您的情况下,您希望在成员加入公会时对其进行跟踪,因此您需要启用 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 的 application page 上为您的机器人启用成员特权意图。 正如您在此 example 中所见。
如果您需要进一步的帮助,这里有几个链接:
度过美好的一天,快乐的编码^^
答案 1 :(得分:1)
顺便说一句,这应该可以工作,它被称为 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