【问题标题】:Delete certain message bot sends删除某些消息机器人发送
【发布时间】:2019-03-01 16:34:03
【问题描述】:

有一个争夺游戏,如果它已解决或未解决,我想删除原始消息。试过 client.delete_message(nameofmsghere) 但它没有删除它。

if message.content.startswith('!scramble'):
    async with aiohttp.post("http://watchout4snakes.com/wo4snakes/Random/RandomWord") as post:
        assert isinstance(post, aiohttp.ClientResponse)
        word = await post.read()
    word = word.decode()
    print(word)
    scrambled = random.sample(word, len(word))
    scrambled = ''.join(scrambled)
    scramblemsg = "The word scramble is: `{}`! You have 10 seconds to solve...".format(scrambled)
    await client.send_message(message.channel, scramblemsg)
def check(m):
    return m.content == word
scrambleloot = random.randint(5,15)
msg = await client.wait_for_message(timeout= 10, check=check)
try:
    if msg:
        await client.send_message(message.channel, "Nice job! {} solved the scramble for ${}! The word was {}!".format(msg.author.mention, scrambleloot, word))
        await client.delete_message(scramblemsg)
        add_dollars(msg.author, scrambleloot)
    else:
        if msg is None:
            await client.send_message(message.channel, "Oops! Nobody solved it. The word was {}!".format(word))
            await client.delete_message(scramblemsg)

【问题讨论】:

    标签: python python-3.x discord discord.py


    【解决方案1】:

    您正在尝试删除一个字符串,该字符串也用于创建消息。它与发送的实际discord.Message 对象无关。你需要捕获那个对象,也就是send_message的返回值

    text = "The word scramble is: `{}`! You have 10 seconds to solve...".format(scrambled)
    scramblemsg  = await client.send_message(message.channel, text)
    ...
    await client.delete_message(scramblemsg)
    

    【讨论】:

    • 现在说得通了。一如既往地感谢您的帮助。
    【解决方案2】:

    您的机器人是否有权删除您正在使用的服务器上的消息?

    您可以通过添加管理消息权限在服务器设置中启用它。

    【讨论】:

      猜你喜欢
      • 2018-11-25
      • 2021-11-13
      • 2019-12-31
      • 2021-10-14
      • 1970-01-01
      • 1970-01-01
      • 2017-12-27
      • 2020-10-26
      • 2019-03-06
      相关资源
      最近更新 更多