python - 如果有人离开 Discord.py,我的队列机器人正在清空队列
问题描述
我正在制作一个不和谐的机器人,它将 6 人排入队列,然后将 3 人随机分配到 2 个团队。然后这些团队进行私人 3v3。
我目前的问题是一旦一个人离开队列,它就会删除其他人,我不知道为什么通过查看我的代码。我有两个数组:
self.total 和 self.mention
self.total 用于存储用户 ID(以便我可以让机器人 DM 他们),而 self.mention 存储他们的标签(以便机器人可以标记它们)。self.qcount 用于统计排队的用户数,一旦达到 6 个用户就关闭队列。
下面是请假命令。
@commands.command()
async def leave(self, ctx):
if len(self.total) > 1:
if ctx.author.mention in self.mention:
self.total = self.total.remove(ctx.author)
self.mention = self.mention.remove(ctx.author.mention)
await ctx.send('You\'ve left the queue.')
self.qcount -= 1
elif len(self.total) == 1:
if ctx.author.mention in self.mention:
self.total = self.total.remove(ctx.author)
self.mention = self.mention.remove(ctx.author.mention)
await ctx.send('You\'ve left the queue.')
self.total = []
self.mention = []
self.qcount = 0
#elif len(self.total) > 1:
#if ctx.author.mention in self.mention:
#self.total = self.total.remove(ctx.author)
#self.mention = self.mention.remove(ctx.author.mention)
#await ctx.send('You\'ve left the queue.')
else:
await ctx.send('You\'re not in the queue.')
return
下面是队列命令:
class queue(commands.Cog):
def __init__(self, client):
self.client = client
self.qcount = 0
self.total = []
self.mention = []
@commands.command()
async def q(self, ctx):
if self.qcount < 6:
self.total
self.mention
guild = ctx.guild
if ctx.author.mention in self.mention:
await ctx.send('You\'re already queued.')
return
else:
#checking if the channel id matches the id allowed.
if ctx.channel.id == 818292898067382297:
self.qcount += 1
await ctx.send('Added to the queue!' + ' ' + f'{ctx.author.mention}')
#adds users to the queue
self.total.append(ctx.author)
self.mention.append(ctx.author.mention)
if self.qcount == 6:
#Takes the teams and shuffles, then splits into two teams.
random.shuffle(self.total)
blue = self.mention[:3]
orange = self.mention [3:6]
blueDM = self.total[:3]
orangeDM = self.total [3:6]
#set p to equal a random user in the queue
p = random.choice(self.total)
username = str((random.randint(1,9999)))
password = str((random.randint(1,9999)))
#Embedded info via dms
HostEmbed = discord.Embed(
title = '6Mans',
description = 'You\'re the host so create the lobby and let the other team know once the lobby is up.\n ' + '**Lobby Name:** OU' + username + '\n' + '**Password:** OU' + password,
colour = discord.Colour.green()
)
BlueEmbed = discord.Embed(
title = '6Mans',
description = '**Host:** ' + f'{p}\n' + 'You\'re on the blue team, please keep an eye on the 6mans channel for an update from the host as to when the lobby is up.\n' + '**Lobby Name:** OU ' + username + '\n' + '**Password:** OU' + password,
colour = discord.Colour.blue()
)
OrangeEmbed = discord.Embed(
title = '6Mans',
description = '**Host:** ' + f'{p}\n' + 'You\'re on the orange team, please keep an eye on the 6mans channel for an update from the host as to when the lobby is up.\n' + '**Lobby Name:** OU' + username + '\n' + '**Password:** OU' + password,
colour = discord.Colour.orange()
)
#dm a random person to be the host + dm other members the lobby info
await p.send(embed = HostEmbed)
for i in blueDM:
await i.send(embed = BlueEmbed)
for i in orangeDM:
await i.send(embed = OrangeEmbed)
#tags the users when queue is ready
await ctx.send('Queue is ready, view the teams below. Check your DMs for lobby info.')
#Voice Channels Created
overwrites = {
guild.default_role: discord.PermissionOverwrite (read_messages=True),
guild.me: discord.PermissionOverwrite(read_messages=True)
}
teamNumber = (random.randint(1,999))
blueVoice = await guild.create_voice_channel('Blue Team' + ' ' + str (teamNumber), user_limit = 3, overwrites=overwrites)
orangeVoice = await guild.create_voice_channel('Orange Team' + ' ' + str(teamNumber), user_limit = 3,overwrites=overwrites)
#Embedded Teams Info
embed1 = discord.Embed(
title = 'Blue Team',
description = 'Your voice channel is: ' + str(blueVoice) + '\n' + f'{blue}',
colour = discord.Colour.blue()
)
await ctx.send(embed=embed1)
embed2 = discord.Embed(
title = 'Orange Team',
description = 'Your voice channel is: ' + str(orangeVoice) + '\n' + f'{orange}',
colour = discord.Colour.orange()
)
await ctx.send(embed=embed2)
#Queue resets, voice channels get deleted
await ctx.send('Setting up the next queue, please wait 5 seconds.')
await asyncio.sleep(3)
self.total.clear()
await asyncio.sleep(3)
self.qcount = 0
await asyncio.sleep(2400)
await blueVoice.delete()
await orangeVoice.delete()
else:
await ctx.send('Please wait a few seconds before trying to queue.')
await asyncio.sleep(3)
self.total.clear()
self.mention.clear()
await asyncio.sleep(3)
self.qcount = 0
return
解决方案
list.remove
是一个始终返回的就地None
操作。因此,当你做类似的事情时
self.total = self.total.remove(ctx.author)
self.total
在你的离开命令中(你实际上在第 5、6、11和12 行中多次犯了同样的错误self.mention
),你正在用None
. 正在做
self.total.remove(ctx.author)
相反会产生预期的结果
推荐阅读
- docker - 将 docker 容器及其数据库容器移动到另一台服务器
- arrays - 如何在 Typescript 4.0.3 中正确扩展 Array 原型?
- html - 图片在桌面上加载,但在网站被推送到 github/netlify 时不会加载
- arrays - 如果插入静态数组是 Space-O(1),那么空间复杂度如何工作?
- java - 依赖注入:如何访问执行命令的相应 ProductView?
- javascript - 使用甜蜜警报而不是自定义 js 确认,但以错误结束
- excel - 当有多个数据表时,如何使用 VBA 更新所选 Excel 数据表中的记录
- sql - 更新表语句 - 违反主键约束
- sql - 在加入 SQL Netezza 时使用窗口聚合函数
- vuejs2 - 不受欢迎的观察者执行顺序