首页 > 解决方案 > 在 discord.py 脚本中使用输入命令

问题描述

我目前正在开发一个机器人,它允许控制台上的某个人(在某种程度上)通过机器人与不和谐的人交流。但是我遇到了一个问题。输入命令不起作用。我真的不在乎它会停止代码,因为无论如何我都会自我托管,并且必须在终端才能真正使用机器人。我遇到的问题是输入不起作用。输入消息的终端中不显示任何内容。如果有帮助,这是我的代码:

enable = True

@client.event
async def on_message(ctx):
    global previousGld
    if previousGld != ctx.guild:
        print(f"{ctx.author}- {ctx.content} \n From- {ctx.guild} \n")
    else:
        print(f"{ctx.author}- {ctx.content} \n")
    if enable == True:
        res = input("Enter response:")
        if res != "n":
            await ctx.send(f"I say: {res}")
            
    previousGld = ctx.guild

标签: pythondiscorddiscord.py

解决方案


找到了解决方案!我使用了@Ceres 关于 aioconsole 的提示,以及一个 try 语句来规避它。感谢所有帮助过的人!这是固定代码。

import discord
import aioconsole

from discord.ext import commands    
client = commands.Bot(command_prefix=".")
client.remove_command('help')

@client.event
async def on_message(message):
    try:
        if message.author == client.user:
            print(f"\nSent- '{message.content}'\n")
        else:
            print(f"\n\n{message.author}- {message.content}\n")
        res = await aioconsole.ainput('Enter response: ')
        if res == "pass":
            pass
        else:
            await message.channel.send(f"I say: {res}")
    except RuntimeError:
        pass

祝你今天过得愉快!:)


推荐阅读