简体   繁体   English

我的 discord 机器人不会发送欢迎消息,但正在阅读其他消息。 我正在使用 discord.py

[英]My discord bot will not send a welcome message, but is reading other messages. I am using discord.py

I am trying to create a basic discord bot and am currently trying to implement a welcome message.我正在尝试创建一个基本的 discord 机器人,目前正在尝试实施欢迎消息。 I am not getting an error message, it just doesn't do anything, and if I try and print a message to the console when a member joins, nothing happens.我没有收到错误消息,它什么也没做,如果我在成员加入时尝试向控制台打印一条消息,什么也不会发生。 The on_message function works just fine, along with the on_ready. on_message function 和 on_ready 一起工作得很好。

#bot

import discord
TOKEN = ('top secret token')

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

@client.event
async def on_ready():
    print(f'{client.user} has connected to Discord!')

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if message.content.startswith('$hello'):
        await message.channel.send('Hello!')

@client.event
async def on_member_join(member):
    await message.channel.send('Welcome!')
    print('New User Detected')




client.run(TOKEN)

Let's see... hmm让我们看看……嗯

does the bot know what message and channel are in the on_member_join event which has kwarg member.机器人是否知道具有 kwarg 成员的on_member_join事件中的messagechannel Nope, it doesn't!不,它没有!

message and channel aren't defined in an on_member_join .消息和频道未在on_member_join中定义。 First, get the channel一、获取渠道

channel = client.get_channel(ChannelID) this will get you the channel you want to send your message to. channel = client.get_channel(ChannelID)这将为您提供要将消息发送到的频道。 after that, we will simply use channel.send("message") to send the message.之后,我们将简单地使用channel.send("message")发送消息。

The final code will look like this:最终代码将如下所示:

@client.event
async def on_member_join(member):
    channel = client.get_channel(ChannelID)
    await channel.send('Welcome!')
    print('New User Detected')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM