Python 如何使bot休眠并停止响应命令(或在_消息上)
我试图发出一个命令,使机器人基本上处于睡眠模式,这意味着使机器人停止响应命令(或在_消息上,如果可能的话) 尝试使用Python 如何使bot休眠并停止响应命令(或在_消息上),python,python-3.x,discord,discord.py,Python,Python 3.x,Discord,Discord.py,我试图发出一个命令,使机器人基本上处于睡眠模式,这意味着使机器人停止响应命令(或在_消息上,如果可能的话) 尝试使用client.pause(Boolean)时没有出现错误我不知道它到底做了什么,但什么也没发生 下面是我目前所处的位置 @client.command(pass_context=True) async def sleep(ctx): client.pause = True @client.command(pass_context=True) async def awake
client.pause(Boolean)
时没有出现错误我不知道它到底做了什么,但什么也没发生
下面是我目前所处的位置
@client.command(pass_context=True)
async def sleep(ctx):
client.pause = True
@client.command(pass_context=True)
async def awake(ctx):
client.pause = False
您可以使用消息上的
覆盖bot自动调用命令,然后为您构建一些逻辑,以确定它不想再接受任何命令(也称为sleep)。但请确保作为机器人所有者,您可以选择唤醒它,否则您将需要重新启动
像下面这样的方法应该可以奏效
我的代码中的假设是您正在使用返工和f字符串,这是可以接受的,但逻辑在这里,几乎不需要更改,我认为它与async
版本兼容
基本控制流程:
bot.py
from discord.ext import commands
class MyBot(commands.bot):
def __init__(self, command_prefix = '!', description=None):
super().__init__(command_prefix, description)
#you could just say False here but if you
#want to start the bot asleep here's a decent way to do it.
self.asleep = kwargs.get('asleep', False)
async def is_owner(obj):
owner = self.application_info().owner
return obj.author == owner
async def on_message(self, message):
ctx = await self.get_context(message)
is_owner = client.is_owner(ctx):
if is_owner or not self.asleep:
self.invoke(ctx)
...
import MyBot
...
bot = MyBot(...)
bot.load_extension("path/to/owner_py_file")
...
owner.py-或保留“仅限bot所有者”命令的任何位置
launcher.py
from discord.ext import commands
class MyBot(commands.bot):
def __init__(self, command_prefix = '!', description=None):
super().__init__(command_prefix, description)
#you could just say False here but if you
#want to start the bot asleep here's a decent way to do it.
self.asleep = kwargs.get('asleep', False)
async def is_owner(obj):
owner = self.application_info().owner
return obj.author == owner
async def on_message(self, message):
ctx = await self.get_context(message)
is_owner = client.is_owner(ctx):
if is_owner or not self.asleep:
self.invoke(ctx)
...
import MyBot
...
bot = MyBot(...)
bot.load_extension("path/to/owner_py_file")
...
我根据bot在调用命令之前如何将它的数据库分配给每个上下文提出了这个方法。这是一个写得非常好的bot,其所有者出于教育目的公开发布了源代码
编辑 以适应您当前的构建 对于您的情况,我假设您只是使用类似于
client=commands.bot(…)
的东西创建了一个bot,这又是一个非常复杂的问题,不允许您利用从子类化bot
中获得的任何能力,但您可以实现以下功能并获得与上面相同的功能:
client = commands.bot(...)
client.asleep = False
然后,对于问题中显示的命令:
@client.command(pass_context=True)
async def sleep(ctx):
client.sleep = True
@client.command(pass_context=True)
async def awake(ctx):
client.asleep = False
然后对于on_消息
覆盖请参考:以帮助解释此功能的工作原理。或者甚至是这个。暗示
如果上述实现对您不起作用,那么您可以给出我在上面链接的答案中描述的方法:
@client.event
async def on_message(message):
is_owner = _is_owner(ctx):
if is_owner or not client.asleep:
await bot.process_commands(message)
请注意我不知道您的模块是如何构造的,但是如果这是在一个类中完成的,并且您在该类中定义了
\u is\u owner(…)
,您将需要使用is\u owner=self.\u is\u owner(…)
尽管这可以在其他地方定义。您可以使用get\u prefix函数更改bot命令,使其不会响应任何公共前缀
backup_prefixes=['.','!','s.','k!']
prefixes = ['.','!','s.','k!']
def get_prefix(bot, msg):
return commands.when_mentioned_or(*prefixes)(bot, msg)
bot = commands.Bot(command_prefix=get_prefix)
@bot.event
async def on_ready():
print(bot.user.name)
@bot.command(pass_context=True)
async def resume(con):
for i in backup_prefixes:
prefixes.append(i)
@bot.command(pass_context=True)
async def pause(con):
prefixes.clear()
# prefixes.append("an unknow prefix so it can't be invoked") #Ex: aslkdjflkj2k4lkj21312kj
prefixes.append(',')
您还可以添加更多的if语句,以确保只有某些用户可以使用该命令使bot静音,从而使其不会响应其他用户。您是否使用任何类型的数据库后端,以便为类似
is_sleep
的内容保存变量?不,我没有使用itNevermind,这将是一个每个实例的变量,虽然我会建议一些持久化变量的方法。说kwargs没有定义,而且我没有使用cogs,所以我可能在从cogs转换的过程中搞砸了。你为什么不使用cogs?此外,如果没有,则使用self.invoke
返回,然后将所有其他“命令”逻辑放在后面。这确实解决了问题,我没有使用cogs,因为我已经开始为我的机器人编写脚本,没有使用cogs,而且脚本太多了,所以要将它们转换为cogs需要一段时间。从长远来看,你会让事情变得更加困难。此外,“这还不能完全解决问题”并没有给我任何信息来帮助你。它不能解决kwargs没有定义的错误,你有没有办法在没有齿轮的情况下重新制作scripot?