将函数的输出作为消息发送 - Discord.py
Send the output of a function as a message - Discord.py
我一直在写一些类似赌博机器人的东西作为一个学习项目,并从一个名为 'Suicide' 的基本游戏开始(简单的游戏,不好的名字)。就游戏而言,它运行良好,但我想将函数 'rolls' 的输出作为消息发送到通道中,命令是 运行 in.
@commands.command()
async def Suicide (self, ctx, StartNum, wager):
ID = (ctx.author.id)
con = sl.connect('balance.db')
with con:
CheckBalance = con.execute(f"SELECT Balance FROM USER WHERE DiscordID = {ID}").fetchone()
CleanBalance = str(CheckBalance).replace("(", "").replace(")" , "").replace("," , "")
if int(wager) > int(CleanBalance):
await ctx.send('You dont have the balance to wager that much')
else:
con.execute(f"""UPDATE USER
SET Balance = Balance - {wager}
WHERE DiscordID = {ID}; """)
await ctx.send(f"Starting game of suicide with a starting number of {StartNum}")
# await asyncio.sleep(2)
StartNum = int(StartNum)
def rolls():
Roll1_result = int(randrange(1, StartNum))
print(f"Roll Result is {Roll1_result} / {StartNum}")
while Roll1_result != 1:
Roll2_result = int(randrange(1, Roll1_result))
print(f"Roll result is {Roll2_result} / {Roll1_result}")
Roll1_result = int(Roll2_result)
else:
print("Reached 1")
rolls()
如果可能的话,有没有人知道这样做的方法。乱七八糟的代码和变量名请见谅
如果需要,这里是我想以嵌入或多行消息的形式发送的输出示例
Roll result is 33 / 53
Roll result is 15 / 33
Roll result is 10 / 15
Roll result is 4 / 10
Roll result is 2 / 4
Roll result is 1 / 2
Reached 1
将print()
替换为ctx.send()
或
pastRolls = []
def rolls():
Roll1_result = int(randrange(1, StartNum))
pastRolls.append(f"Roll Result is {Roll1_result} / {StartNum}")
while Roll1_result != 1:
Roll2_result = int(randrange(1, Roll1_result))
pastRolls.append(f"Roll result is {Roll2_result} / {Roll1_result}")
Roll1_result = int(Roll2_result)
else:
pastRolls.append("Reached 1")
rolls()
for thing in pastRolls:
ctx.send(thing)
我一直在写一些类似赌博机器人的东西作为一个学习项目,并从一个名为 'Suicide' 的基本游戏开始(简单的游戏,不好的名字)。就游戏而言,它运行良好,但我想将函数 'rolls' 的输出作为消息发送到通道中,命令是 运行 in.
@commands.command()
async def Suicide (self, ctx, StartNum, wager):
ID = (ctx.author.id)
con = sl.connect('balance.db')
with con:
CheckBalance = con.execute(f"SELECT Balance FROM USER WHERE DiscordID = {ID}").fetchone()
CleanBalance = str(CheckBalance).replace("(", "").replace(")" , "").replace("," , "")
if int(wager) > int(CleanBalance):
await ctx.send('You dont have the balance to wager that much')
else:
con.execute(f"""UPDATE USER
SET Balance = Balance - {wager}
WHERE DiscordID = {ID}; """)
await ctx.send(f"Starting game of suicide with a starting number of {StartNum}")
# await asyncio.sleep(2)
StartNum = int(StartNum)
def rolls():
Roll1_result = int(randrange(1, StartNum))
print(f"Roll Result is {Roll1_result} / {StartNum}")
while Roll1_result != 1:
Roll2_result = int(randrange(1, Roll1_result))
print(f"Roll result is {Roll2_result} / {Roll1_result}")
Roll1_result = int(Roll2_result)
else:
print("Reached 1")
rolls()
如果可能的话,有没有人知道这样做的方法。乱七八糟的代码和变量名请见谅
如果需要,这里是我想以嵌入或多行消息的形式发送的输出示例
Roll result is 33 / 53
Roll result is 15 / 33
Roll result is 10 / 15
Roll result is 4 / 10
Roll result is 2 / 4
Roll result is 1 / 2
Reached 1
将print()
替换为ctx.send()
或
pastRolls = []
def rolls():
Roll1_result = int(randrange(1, StartNum))
pastRolls.append(f"Roll Result is {Roll1_result} / {StartNum}")
while Roll1_result != 1:
Roll2_result = int(randrange(1, Roll1_result))
pastRolls.append(f"Roll result is {Roll2_result} / {Roll1_result}")
Roll1_result = int(Roll2_result)
else:
pastRolls.append("Reached 1")
rolls()
for thing in pastRolls:
ctx.send(thing)