【问题标题】:Use the input command in a discord.py script在 discord.py 脚本中使用输入命令
【发布时间】:2021-03-19 12:40:27
【问题描述】:
我目前正在开发一个机器人,它允许控制台上的某人(在某种程度上)通过机器人与不和谐的人交流。但是我遇到了一个问题。输入命令不起作用。我真的不在乎它会停止代码的运行,因为无论如何我都会自我托管,并且必须在终端上才能真正使用机器人。我遇到的问题是输入不起作用。输入消息的终端中不显示任何内容。如果有帮助,这是我的代码:
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
【问题讨论】:
标签:
python
discord
discord.py
【解决方案1】:
找到了解决办法!我使用了@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
祝你有美好的一天! :)
【解决方案2】:
这是因为您没有为您的on_message 事件传递ctx。相反,请传入message。此外,正如@Ceres 指出的那样,您应该更改输入法。确保在终端中下载 aioconsole 和 pip install aioconsole。
import aioconsole
@client.event
async def on_message(message):
global previousGld
if previousGld != message.guild:
print(f"{message.author}- {message.content} \n From- {message.guild} \n")
else:
print(f"{message.author}- {message.content} \n")
res = await aioconsole.ainput("Enter response:")
if res != "n":
await message.channel.send(f"I say: {res}")
previousGld = message.guild