【问题标题】:My discord bot will not send a welcome message, but is reading other messages. I am using discord.py我的不和谐机器人不会发送欢迎消息,但正在阅读其他消息。我正在使用 discord.py
【发布时间】:2021-06-01 18:03:53
【问题描述】:

我正在尝试创建一个基本的不和谐机器人,并且目前正在尝试实施欢迎消息。我没有收到错误消息,它什么也没做,如果我在成员加入时尝试向控制台打印一条消息,什么也不会发生。 on_message 函数和 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)

【问题讨论】:

    标签: python python-3.x discord


    【解决方案1】:

    让我们看看……嗯

    机器人是否知道 messagechannel 在具有 kwarg 成员的 on_member_join 事件中是什么。不,它没有!

    消息和频道未在on_member_join 中定义。首先,获取频道

    channel = client.get_channel(ChannelID) 这将为您提供您想要发送消息的频道。 之后,我们将简单地使用channel.send("message") 发送消息。

    最终代码如下所示:

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

    【讨论】:

    • 嗨,我使用了那个确切的代码,但它仍然没有给我消息。我还尝试将频道 ID 放在您拥有 ChannelID 的位置,但仍然无法发送。
    • 我搞定了,这是权限问题。
    猜你喜欢
    • 2019-12-23
    • 2021-04-27
    • 2017-10-30
    • 2020-12-06
    • 2020-11-20
    • 2021-09-11
    • 2022-01-18
    • 2021-08-17
    • 2022-01-23
    相关资源
    最近更新 更多