Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在discord.py脚本中使用输入命令_Python_Discord_Discord.py - Fatal编程技术网

Python 在discord.py脚本中使用输入命令

Python 在discord.py脚本中使用输入命令,python,discord,discord.py,Python,Discord,Discord.py,我目前正在开发一个机器人,它允许控制台上的人通过机器人与不和谐的人进行某种程度的交流。然而,我遇到了一个问题。输入命令不起作用。我真的不在乎它是否会停止代码的运行,因为我无论如何都会自动托管,并且必须在终端上才能真正使用bot。我遇到的问题是输入不起作用。终端中不显示输入信息的任何内容。以下是我的代码(如果有帮助): enable = True @client.event async def on_message(ctx): global previousGld if prev

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

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

这是因为您没有为on_消息事件传递ctx。相反,传递消息。此外,正如@Ceres所指出的,您应该更改您的输入方法。确保使用pip下载aioconsole并在终端中安装aioconsole

导入aioconsole @客户端事件 _message上的异步定义消息: 全球前GLD 如果以前的GLD!=message.guild: printf{message.author}-{message.content}\n来自-{message.guild}\n 其他: printf{message.author}-{message.content}\n res=等待aioconsole.ainputEnter响应: 如果res!=n: 等待message.channel.sendfI说:{res} previousGld=message.guild
找到了解决办法!我使用了@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

祝你今天愉快

尝试使用而不是InputPut阻塞,因此仍会中断command@FluxedScript我已经编辑过了。