我这里有一些代码,可以使用命令通过计时器开始拍卖。
一切正常。
在auction
命令下是autobid
命令。
我要执行的操作是获取autobid
命令以完全停止auction
命令中的计时器,从而结束拍卖。
我尝试了多种方法,包括全局变量。我也在很长一段时间里一直在寻找我的问题,我似乎找不到解决办法。
到目前为止我学到的东西:
autobid
命令的确使机器人说了应该说的话,autobid
并没有停止auction
命令中的倒计时。 有人可以帮我这个忙,还是可以让我知道我做错了什么?
更新的代码:
@bot.command(pass_context=True)
@commands.has_any_role("Admin", "Mods")
async def auction(ctx, member: discord.Member, message1, number, number2, num: int, number4):
if member is None:
member = ctx.message.author
auction = discord.Embed(title='Auction Host: ', description=(member.mention), color=0x00ff00)
auction.add_field(name='Cards/Items: ', value=(message1), inline=False)
auction.add_field(name='Starting Bid: ', value=(number), inline=False)
auction.add_field(name='Increment: ', value=(number2), inline=False)
auction.add_field(name='Time Left: ', value=(num), inline=False)
auction.add_field(name='Auto Bid: ', value=(number4), inline=False)
message = await bot.say(embed=auction)
perms = discord.PermissionOverwrite(send_messages=True)
server = bot.get_server("459456116208959492")
rank = discord.utils.get(ctx.message.server.roles, name='Members')
await bot.edit_channel_permissions(channel=ctx.message.channel, target=rank, overwrite=perms)
await bot.say("@here")
if num > 10801:
return await bot.say("I don't think I'm allowed to go above 3 hours!")
elif num <= 0:
return await bot.say("Can't be zero or negative.")
id = ctx.message.channel.id
if running.get(id, 0) > 0:
# if the countdown while loop is already running,
# don't start another one in the same channel
return await bot.say("Already running.")
running[id] = num
while True:
running[id] = running[id] - 1
if running[id] <= 0:
auction3 = discord.Embed(title='Auction Host: ', description=(member.mention), color=0x00ff00)
auction3.add_field(name='Cards/Items: ', value=(message1), inline=False)
auction3.add_field(name='Starting Bid: ', value=(number), inline=False)
auction3.add_field(name='Increment: ', value=(number2), inline=False)
auction3.add_field(name='Time Left: ', value="Auction Over!", inline=False)
auction3.add_field(name='Auto Bid: ', value=(number4), inline=False)
await bot.edit_message(message, new_content=None, embed=auction3)
perms = discord.PermissionOverwrite(send_messages=False)
server = bot.get_server("459456116208959492")
rank = discord.utils.get(ctx.message.server.roles, name='Members')
await bot.edit_channel_permissions(channel=ctx.message.channel, target=rank, overwrite=perms)
break
auction2 = discord.Embed(title='Auction Host: ', description=(member.mention), color=0x00ff00)
auction2.add_field(name='Cards/Items: ', value=(message1), inline=False)
auction2.add_field(name='Starting Bid: ', value=(number), inline=False)
auction2.add_field(name='Increment: ', value=(number2), inline=False)
auction2.add_field(name='Time Left: ', value="{} seconds".format(running[id]), inline=False)
auction2.add_field(name='Auto Bid: ', value=(number4), inline=False)
await bot.edit_message(message, new_content=None, embed=auction2)
await sleep(1)
await bot.say(member.mention + " your auction of " + str(message1) + " has ended!")
@bot.command(pass_context=True)
async def autobid(ctx, number6, member: discord.Member = None):
running[ctx.message.channel.id] = 0
if member is None:
member = ctx.message.author
await bot.say(str(ctx.message.author.mention) + ' has won the auction with an autobid of: ' + str(number6).format(member))
答案 0 :(得分:0)
如果我的评论中描述的上下文反映了您的情况:
根据我对你的问题的理解,你基本上想要
autobid
命令将成为终止action
的“停止”命令 命令的while循环。
这是一个简单的“模拟”代码,您可以从中学习并适应您的实际代码。该示例包含两个命令:.run
和.stop
。 .run
将启动一个无限的消息传递循环,而.stop
将停止该循环。这只是一个基本框架,因此您需要进行更改以使其适合您的需求。
from discord.ext import commands
from asyncio import sleep
bot = commands.Bot('.')
running = {}
@bot.command(pass_context=True)
async def run(ctx):
channel = ctx.message.channel
if running.get(channel.id, False):
# if the infinite running while loop is already running,
# don't start another one in the same channel
return await bot.send_message(channel, "already running")
running[channel.id] = True
while running[channel.id]:
# prints a message every second until `.stop` is typed
await bot.send_message(channel, "still running")
await sleep(1)
@bot.command(pass_context=True)
async def stop(ctx):
channel = ctx.message.channel
running[channel.id] = False
await bot.send_message(channel, "the loop had been stopped.")
bot.run('TOKEN')
另一个例子:
from discord.ext import commands
from asyncio import sleep
bot = commands.Bot(command_prefix='!')
running = {}
@bot.command(pass_context=True)
async def countdown(ctx, num: int):
if num > 10800:
return await bot.say("Too long.")
elif num <= 0:
return await bot.say("Can't be zero or negative.")
id = ctx.message.channel.id
if running.get(id, 0) > 0:
# if the countdown while loop is already running,
# don't start another one in the same channel
return await bot.say("Already running.")
running[id] = num
while True:
running[id] -= 1
if running[id] <= 0:
# do something when counter's at zero
break
# do something...
await sleep(1)
await bot.say("Done.")
@bot.command(pass_context=True)
async def stop(ctx):
running[ctx.message.channel.id] = 0
await bot.say("The countdown had been stopped.")
bot.run('TOKEN')
如果您不想要(不知道为什么不想要),我将先前的代码设置为与频道无关:
from discord.ext import commands
from asyncio import sleep
bot = commands.Bot(command_prefix='!')
running = 0
@bot.command()
async def countdown(num: int):
if num > 10800:
return await bot.say("Too long.")
elif num <= 0:
return await bot.say("Can't be zero or negative.")
if running > 0:
# if the countdown while loop is already running,
# don't start another one in the same channel
return await bot.say("Already running.")
running = num
while True:
running -= 1
if running <= 0:
# do something when counter's at zero
break
# do something...
await sleep(1)
await bot.say("Done.")
@bot.command()
async def stop():
running = 0
await bot.say("The countdown had been stopped.")
bot.run('TOKEN')