Discord.py |取消固定命令
Discord.py | Unpin command
基本上我想做的是做一个有点像这样的命令:
用户:>取消固定 10
Bot 取消固定所述金额和 returns:#channel
中取消固定的金额
到目前为止我所做的是:
@Bot.command()
async def unpin(ctx, amount = 0):
await ctx.message.delete()
channel = ctx.channel
x = 0
amount = int(amount)
if amount == 0:
await ctx.send("How many messages do you want to unpin, max is 50.")
if amount > 50:
await ctx.send("Maximum amount is 50")
else:
pins = await channel.pins()
while x <= amount:
for message in pins:
await message.unpin()
x+=1
await ctx.send(f"Unpinned {x} messages from #{channel}")
这里的问题是机器人取消固定每条消息而不是给定的数量。我该如何更改它以使其取消固定给定的数量?
让它更像 pythonic。不要使用 while x <= amount: ... x += 1
,而是使用 for 循环。另外,不要double-loop。第二个循环遍历所有引脚和 un-pins 所有引脚。一个更好的系统的例子可能如下:
...
else:
for i in range(amount):
pins = await channel.pins()
await pins[-1].unpin()
await ctx.send(f"Unpinned {x} messages from #{channel}")
基本上我想做的是做一个有点像这样的命令:
用户:>取消固定 10
Bot 取消固定所述金额和 returns:#channel
中取消固定的金额到目前为止我所做的是:
@Bot.command()
async def unpin(ctx, amount = 0):
await ctx.message.delete()
channel = ctx.channel
x = 0
amount = int(amount)
if amount == 0:
await ctx.send("How many messages do you want to unpin, max is 50.")
if amount > 50:
await ctx.send("Maximum amount is 50")
else:
pins = await channel.pins()
while x <= amount:
for message in pins:
await message.unpin()
x+=1
await ctx.send(f"Unpinned {x} messages from #{channel}")
这里的问题是机器人取消固定每条消息而不是给定的数量。我该如何更改它以使其取消固定给定的数量?
让它更像 pythonic。不要使用 while x <= amount: ... x += 1
,而是使用 for 循环。另外,不要double-loop。第二个循环遍历所有引脚和 un-pins 所有引脚。一个更好的系统的例子可能如下:
...
else:
for i in range(amount):
pins = await channel.pins()
await pins[-1].unpin()
await ctx.send(f"Unpinned {x} messages from #{channel}")