【问题标题】:Deleting a bot's message in discord.py在 discord.py 中删除机器人的消息
【发布时间】:2018-08-16 00:34:03
【问题描述】:

我已经进行了所有正确的导入,并尝试从其他帖子中寻找答案,但它似乎不太适合我的问题。我正在尝试随机发送一条消息,我可以做到。但是,经过一定的冷却时间后,我似乎无法删除这些消息。然而,冷却时间不是问题。它正在删除机器人消息。我知道如何删除用户的消息,但我对如何删除机器人消息一无所知。你能帮忙的话,我会很高兴。这是我的代码,但我的令牌 ID 和导入除外。

async def background_loop():
await client.wait_until_ready()
while not client.is_closed:
    channel = client.get_channel('397920718031159318')
    messages = ["A random cat has appeared", "oh look its a cate"]
    await client.send_message(channel, random.choice(messages))
    time.sleep(3) #I am using this as the cool down time to delete the 
                  #message
    await client.delete_message(messages)
    await asyncio.sleep(4)

【问题讨论】:

  • 在黑暗中拍摄,但是您是否尝试过遍历消息列表并将每条单独的消息发送到 delete_message 方法?如果您只想传递整个列表,您可能还想尝试使用 delete_messages 方法。

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


【解决方案1】:

现在(discord.py v1.3.0),只需:

import discord

from discord.ext.commands import Bot


bot = Bot()

@bot.command()
async def ping(ctx):
    await ctx.send('pong!', delete_after=5)

bot.run('YOUR_DISCORD_TOKEN')

【讨论】:

  • 我认为他们没有使用discord.pyBot模块...
【解决方案2】:

这是重写版本,但仍然有效:

channel = client.get_channel('397920718031159318')
messages = ["A random cat has appeared", "oh look its a cate"]
await(await client.send_message(channel, random.choice(messages))).delete(delay=3)
await asyncio.sleep(4)

【讨论】:

    【解决方案3】:

    对于 Discord.py 版本 1.0.0 及更高版本,现在是: 我知道您没有问如何删除发件人的消息,但无论如何它都在这里...

    import asyncio
    channel = 397920718031159318 #get the channel
    ## send the message
    message = await ctx.send('message')
    ## wait for 4 seconds
    await asyncio.sleep(4)
    ## delete the message
    await message.delete()
    
    ## ^^ To delete the Bots message ^^ ##
    ## vv To delete the senders message vv ##
    
    ## get the message
    message = ctx.message
    ## wait for 4 seconds again
    await asyncio.sleep(4)
    ## delete the message
    await message.delete()
    
    #############################
    
    ## You can edit the message in about the same way:
    
    message = await ctx.send('Old Message')
    await asyncio.sleep(0.5)  # this is so the message has time to be read
    await message.edit(content="New Message")
    
    

    【讨论】:

    • 这是重写,异步已被弃用
    【解决方案4】:
    while not client.is_closed:
        channel = client.get_channel('397920718031159318')
        messages = ["A random cat has appeared", "oh look its a cate"]
        message = await client.send_message(channel, random.choice(messages))
        await asyncio.sleep(3) 
        await client.delete_message(message)
        await asyncio.sleep(4)
    

    您必须捕获send_message 产生的消息对象,然后将该对象发送给delete_message

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多