首页 > 解决方案 > 尝试使用 discord.py 制作老虎机机器人

问题描述

这是我下面的代码:

import discord
import random
import time

tst = [1, 2, 3]

if "!roll" in message.content.lower():
    first = [await message.channel.send(str(random.choice(tst)).format(message))]
    time.sleep(1)
    second = [await message.channel.send(str(random.choice(tst)).format(message))]
    time.sleep(1)
    third = [await message.channel.send(str(random.choice(tst)).format(message))]
    time.sleep(1)
    if first == second == third:
        await message.channel.send("you win!".format(message))

该代码有效,但在您获胜时不会发送消息。

我想我做错了什么,但无法找出编写代码的正确方法。

标签: pythondiscord.py

解决方案


它从不发送任何东西,因为 if 语句永远不会为真。您正在比较discord.Message三个消息的三个实例列表。这些都是不同的,所以[message1] == [message2] == [message3]永远不会True。而是比较这些值。

此外,.format(message)根本不做任何事情,我不确定你期望它做什么。您应该删除它(或让它做一些有用的事情)。

first = random.choice(tst)
second = random.choice(tst)
third = random.choice(tst)

await message.channel.send(str(first))
await message.channel.send(str(second))
await message.channel.send(str(third))

if first == second == third:
    await message.channel.send("You win!")

还,

如果在 message.content.lower() 中出现“!roll”:

考虑使用commands而不是手动解析所有内容。有一个关于它们如何在GitHub repo上工作的基本示例。


推荐阅读