AttributeError:模块“bot”没有属性“run_discord_bot”

问题描述 投票:0回答:0

使用 Python 3.10.11。尝试基于 this youtube tutorial 制作一个简单的 discord 机器人,我完全按照指定的方式做了所有事情,但是在我的

main.py

中不断出现这个错误
Exception has occurred: AttributeError
module 'bot' has no attribute 'run_discord_bot'
  File "C:\Users\ME\Documents\python stuff\my python\main.py", line 4, in <module>
    bot.run_discord_bot()
AttributeError: module 'bot' has no attribute 'run_discord_bot'

这是我的 main.py:

import bot

if __name__ == '__main__':
    bot.run_discord_bot()

这是我的机器人.py

import discord
import responses

# Send messages
async def send_message(message, user_message, is_private):
    try:
        response = responses.handle_response(user_message)
        await message.author.send(response) if is_private else await message.channel.send(response)

    except Exception as e:
        print(e)

def run_discord_bot():
    TOKEN = 'my token is here, removed for privacy'
    client = discord.Client()

    @client.event
    async def on_ready():
        print(f'{client.user} is now running!')

    @client.event
    async def on_message(message):
        # Make sure bot doesn't get stuck in an infinite loop
        if message.author == client.user:
            return

        # Get data about the user
        username = str(message.author)
        user_message = str(message.content)
        channel = str(message.channel)

        # Debug printing
        print(f"{username} said: '{user_message}' ({channel})")

        # If the user message contains a '?' in front of the text, it becomes a private message
        if user_message[0] == '?':
            user_message = user_message[1:]  # [1:] Removes the '?'
            await send_message(message, user_message, is_private=True)
        else:
            await send_message(message, user_message, is_private=False)

    client.run(TOKEN)

我需要知道如何修复开头指定的错误。

预期的机器人运行,不应该是一个错误,因为该属性存在。查了一下这个问题,但没有解决我的问题。

discord discord.py bots attributeerror
© www.soinside.com 2019 - 2024. All rights reserved.