Discord Welcome Bot(Python)不工作,即使没有错误

2024-05-13 08:38:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用python制作一个discord bot,当有人加入服务器时,它将发送一条简单的欢迎消息。我查阅了许多youtube教程,并尝试了许多不同的方法使其工作,但由于某些原因,它不起作用

代码中没有错误。我的代码中有一个on_message(message)函数,只是为了测试bot,该函数工作得非常好

另一方面,on_member_join函数是不起作用的函数。我在我的discord开发者门户上启用了member intents功能,并且bot具有管理员权限。我试着将此消息作为意图发送,但仍然不起作用

编辑1:根据评论,我删除了客户的第二个定义。仍然无法让机器人显示欢迎消息。这个on_message(message)是有效的

编辑2:我解决了这个错误。这个错误是因为我重写了客户端的定义,也因为输入错误。感谢所有帮助过我的人

import discord
from discord.ext import commands

token = "I have my token here"


intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents = intents)



@client.event
async def on_ready():
    print("Bot is ready")


#respond hello to hi (for testing if the bot works)
@client.event
async def on_message(message):
    if message.content == 'hi':
        await message.channel.send('Hello')



#main function
@client.event
async def on_member_join(member):
    guild = client.get_guild(I have my server ID here)                        #server id
    channel = guild.get_channel(I have my channel ID here)                     #channel id
    await channel.send(f'Welcome to the server {member.mention}!  ')     #edit this line to edit message



client.run(token)


Tags: 函数clienttoken消息messagehereonmy