Windows命令行(Windows Command Line)
当有人直接输入到windows命令行时,是否有人知道如何摆脱for循环? 我知道你可以使用gotos和标签在批处理文件中突破它,但我找不到任何关于在命令行中突破一个的东西。 这是一个简单的例子:
C:> for /l %i in (1,0,1) do @ping -n 1 google.com || (echo ^G & msg user "Google is down!" & QUIT)
这应该无限ping google.com。 如果它失败了,它会发出嘟嘟声(echo ^ G),向用户“user”显示一个消息框,上面写着“Google已关闭!”和QUIT。 我不知道如何做退出部分。 我想我可以做像taskkill / f / im cmd.exe这样的东西,但我一直在寻找更优雅的东西。 有小费吗?
Does anyone know how to break out of a for loop when it's typed directly into the windows command-line? I know you can use gotos and labels to break out of it when it's in a batch file, but I can't find anything about breaking out of one on the command line. Here's a simple example:
C:> for /l %i in (1,0,1) do @ping -n 1 google.com || (echo ^G & msg user "Google is down!" & QUIT)
This should infinitely ping google.com. If it ever fails, it beeps (echo ^G), displays a message box to the user "user" that says "Google is down!", and QUITs. I don't know how to do the quit part though. I guess I could do something like taskkill /f /im cmd.exe, but I was looking for something more elegant. Any tips?
原文:https://stackoverflow.com/questions/2437071
最满意答案
由于
discord.py
的事件驱动模型,这实际上比听起来更棘手。 我们的命令将接受我们的查询的一些参数,并返回给用户他们选择的逐项列表。 我们将该用户和该列表存储在字典中。 然后,在我们的on_message
事件中,我们将针对我们关心的作者的字典检查每条消息,并尝试将其消息解释为列表中的选项,直到它们进行有效选择。waiting_for = {} @bot.command(pass_context=True) async def fort(ctx, arg): # database setup stuff names = cursor.execute("SELECT name FROM forts WHERE name LIKE '" + str(arg) + "%';") if not names: await bot.say("That fort does not exist") elif len(names) == 1: await process_fort(name) else: choices = "\n".join("{}. {}".format(i, x) for i, x in enumerate(names, start=1)) await bot.say("Your choices are") await bot.say(choices) waiting_for[ctx.message.author.id] = names @bot.event async def on_message(message): id = message.author.id if id in waiting_for: if message.content.isdigit(): names = waiting_for[id] selection = int(message.content) if 0 < selection < len(names): del waiting_for[id] await process_fort(name) return await bot.process_commands(message) # Whatever you want to do with the name once you have it async def process_fort(name): ...
如果这看起来很复杂,您也可以向用户回显所有可能性,直到输入仅返回一个结果的查询。
This is actually trickier than it sounds, because of
discord.py
's event driven model. Our command will accept some argument to our query, and return to the user an itemized list of their choices. We'll store that user and that list in a dictionary. Then, in ouron_message
event we'll check each message against the dict of authors we care about, and try to interpret their messages as choices in the list until they make a valid selection.waiting_for = {} @bot.command(pass_context=True) async def fort(ctx, arg): # database setup stuff names = cursor.execute("SELECT name FROM forts WHERE name LIKE '" + str(arg) + "%';") if not names: await bot.say("That fort does not exist") elif len(names) == 1: await process_fort(name) else: choices = "\n".join("{}. {}".format(i, x) for i, x in enumerate(names, start=1)) await bot.say("Your choices are") await bot.say(choices) waiting_for[ctx.message.author.id] = names @bot.event async def on_message(message): id = message.author.id if id in waiting_for: if message.content.isdigit(): names = waiting_for[id] selection = int(message.content) if 0 < selection < len(names): del waiting_for[id] await process_fort(name) return await bot.process_commands(message) # Whatever you want to do with the name once you have it async def process_fort(name): ...
If that seems to complicated, you could also just echo all the possibilities to the user until the enter a query that only returns one result.
相关问答
更多-
为什么我的Python代码看不到导入的discord.py模块?(Why doesn't my Python code see the imported discord.py module?)[2022-03-22]
您的ide可能使用了与您下载discord.py的不同的python解释器。 您需要更改sdk设置。 请遵循以下教程: https : //www.jetbrains.com/help/idea/2017.1/configuring-python-interpreter-for-a-project.html 要为Python模块配置Python解释器,请按照下列步骤操作 打开“项目结构”对话框(例如Ctrl + Shift + Alt + S)。 在“项目结构”对话框中,单击“项目设置”下的“模块”节点。 ... -
要回答你的第一个问题,是的,你可以在一个单独的文件中写入命令,并让它们仍然注册为main。 在discord.py中,这被称为使用齿轮。 这是一个例子 。 假设这就是你的代码,你的代码有几个问题。 首先,你必须总是await协程函数才能使用它们 - > await client.send_message("Hello") ... await client.say("Hello")其次,我不确定创建一个“userhandler”是一个好主意。 特别是你试图考虑的方式有更简单的方法来获取用户对象。 最后,请参阅 ...
-
Bash:Python3:找不到命令(Windows,discord.py)(Bash: Python3: command not found (Windows, discord.py))[2023-01-11]
在Windows上,python可执行文件的正常名称是python.exe (控制台程序)或pythonw.exe (用于GUI程序)。 python可执行文件有时在某些平台上称为python3 ,其中默认( python )是旧的python 2.在许多基于UNIX(包括Linux和OS X)的系统上,系统实用程序使用python 2,更改它可能有这些平台上的不良后果,因此名称为“python3”。 在Windows上你应该没问题 - 在Windows上还有其他问题,除非你尝试使用多个python版本,否 ... -
discord.py中的错误400(Error 400 in discord.py)[2023-10-13]
由于您正在使用discord api,如果您阅读了client.send_message的描述,如果您在embed发送的消息超过2000个chrs,则discord将引发400请求错误。 对于Discord,角色限制为2000。 正如您所看到的,它实际上并不是真正的错误, discord.errors.HTTPException: BAD REQUEST (status code: 400) 。 这是discord API的自定义错误。 要更正它,您可以将消息拆分为小于2000个chrs的嵌入并单独发送。 ... -
对于异步只是做 await client.delete_message(message) 否则重写只是做 await message.delete() for async just do await client.delete_message(message) otherwise for rewrite just do await message.delete()
-
由于discord.py的事件驱动模型,这实际上比听起来更棘手。 我们的命令将接受我们的查询的一些参数,并返回给用户他们选择的逐项列表。 我们将该用户和该列表存储在字典中。 然后,在我们的on_message事件中,我们将针对我们关心的作者的字典检查每条消息,并尝试将其消息解释为列表中的选项,直到它们进行有效选择。 waiting_for = {} @bot.command(pass_context=True) async def fort(ctx, arg): # database setup ...
-
Python Discord bot在交给功能时不会说些什么(Python Discord bot won't say something when handed to function)[2022-01-21]
bot.say是一个协程 ,所以你必须yield from或await结果。 另外,你不能在命令之外使用bot.say 你应该尝试从控制机器人的代码中分离出游戏运行的所有代码。 bot.say is a coroutine, so you must either yield from or await the result. Additionally, you cannot use bot.say outside of commands You should try to separate all of t ... -
delete_channel('kick')将无法工作,因为您需要传入channel对象而不是字符串。 您不需要使用discord.utils来获取所需的频道。 create_channel返回一个通道对象,因此您应该可以使用它。 但是,你确实需要获得你要踢的Member对象。 您还错误地引用了message.server而不是ctx.message.server @bot.command(pass_context=True) async def kick(ctx, victim): victim_ ...
-
您可以运行aiohttp以及任何使用asyncio的库。 或者你可以在单独的线程中启动aiohttp。 I've found a solution of sorts, I had to use an async web server, namely the poorly documented Kyoukai - http://kyoukai.readthedocs.io/en/latest/ I can now silmeoutaneously interface with the Discord API ...
-
您的问题似乎主要是关于混合同步和异步代码。 有两种可能性: 1)如果你的非异步例程不需要阻塞,只需要调度一些异步任务(例如send_message )稍后运行,那么他们可以简单地调用get_event_loop().create_task() 。 如果您希望在异步操作完成时调用其他(非异步)例程,您甚至可以在返回的任务上使用add_done_callback 。 (如果要运行的例程也是非异步的,则使用get_event_loop().call_soon() 。) 2)如果你的非异步例程绝对必须阻塞(其中包括 ...