[英]Implementing a queue function in discord.py
我正在尝试使用 discord.py 实现一个允许用户进入队列的命令。 一旦队列达到 6 个总用户,它就会关闭。 现在我只是在测试如何增加队列。 当前,如果 count.= 6,则队列返回默认计数 = 0,但是。 我不知道如何再次运行队列命令而不运行整个 function,基本上。 一旦用户启动队列命令,我需要它来保存他们在队列中的位置,同时还允许更多用户进入。 同时避免返回到函数的开头。
我知道这看起来很简单,但我不知道该怎么做。 我尝试将排队的成员转换为 integer 以比较排队的成员的总值与 6 个用户限制,但无法将“message.author”解析为 integer。
@client.command()
async def queue(ctx):
count = 0
while count <= 6:
await ctx.send('Added to the queue!' f'{ctx.author.mention}')
count += 1
#member = ctx.author.mention
while count != 6:
return
else:
await ctx.send('Queue full')
谢谢您的帮助。
您可以简单地在 function 之外有一个变量,并在 function 内递增它,如下所示
count = 0
@client.command()
async def queue(ctx):
global count
if count < 6: # Smaller than 6 so that only 6 people can enter
await ctx.send('Added to the queue!' f'{ctx.author.mention}')
count += 1
#member = ctx.author.mention
else:
await ctx.send('Queue full')
您还可以查看不和谐的 api wait_for如果您的程序应该在触发命令后“等待”某个事件发生,这可能很有用
每次调用命令时,在开始时调用count = 0
会将您的队列重置为 0
您要做的是将队列 state 保存在 memory 或磁盘上。 在这种情况下解决它的肮脏方法是使用全局变量,但您通常希望避免使用这些变量。 这就是为什么
qcount = 0
@client.command()
async def queue(ctx):
global qcount
if qcount <= 6:
qcount += 1
await ctx.send('Added to the queue!' f'{ctx.author.mention}')
else:
await ctx.send('Queue full')
您真正想要做的是将机器人打包到 class (Cog) 中并在其 init 中启动队列:
class Botname(commands.Cog):
def __init__(self, client):
self.client = client
self.qcount = 0
@commands.command()
async def queue(self, ctx):
if self.qcount <= 6:
self.qcount += 1
await ctx.send('Added to the queue!' f'{ctx.author.mention}')
else:
await ctx.send('Queue full')
return
if __name__ == '__main__':
client.add_cog(Botname(client))
client.run(TOKEN)
或者您可以使用 SQL 数据库来存储queue
值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.