python - 在 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
解决方案
找到了解决方案!我使用了@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
祝你今天过得愉快!:)
推荐阅读
- python - 根据文件所在文件夹的创建日期重命名文件
- php - 激活邮件订阅者插件时出现严重错误
- javascript - 如何使用 Javascript 过滤功能根据用户输入过滤引导卡
- angular - 使用 Traefik IngressRoute 为 Angular 服务重写目标后无法匹配路由
- elixir - Phoenix 阻止 conn 从 Liveview 重定向
- excel - 如何以数学方式计算日期时间值的未格式化值
- excel - Excel:可以在不保护工作表的情况下将单元格设为只读吗?
- mysql - 创建 MySQL 存储过程时,如何避免使用不断变化的 ip 设置 DEFINER?
- reactjs - React Apollo 在变异后延迟更新缓存
- javascript - 每 x 秒播放一次 gif,而不是不断循环播放