【问题标题】:Delete Messages with Python Discord bot?使用 Python Discord 机器人删除消息?
【发布时间】:2019-02-20 05:19:23
【问题描述】:

我正在尝试制作一个 Python Discord Bot,它首先可以删除频道中的消息。我希望它以终结者 3 为主题,所以它会从用户说天网开始,然后机器人要求激活 Y 或 N?当用户输入Y时,它会删除频道中的所有消息,如果用户输入N,它会说审判日是不可避免的。任何帮助将不胜感激。

import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio

token = 'Token'

Client = discord.Client()
client = commands.Bot(command_prefix = '!')


@client.event
  async def on_ready():
     print("Skynet Online")

@client.event

  async def on_message(message):
    if message.content == 'skynet':
        await client.send_message(message.channel, 'Execute Y/N?')

@asyncio.coroutine
  async def delete_messages(messages):
    if message.content == 'Y':
        await client.delete_messages()


client.run('Token')

【问题讨论】:

  • 发生的确切问题是什么?你从来没有问过一个具体的问题让人们回答。
  • 消息内容或响应“Y”不会触发机器人删除消息。但是,键入 skynet 时的第一条消息确实会使机器人输出 Y 或 N?
  • 已经有一些关于使用Client.wait_for_message 进行交互命令的问题。

标签: python bots discord discord.py


【解决方案1】:
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio

@has_permissions(manage_messages=True, read_message_history=True)
@bot_has_permissions(manage_messages=True, read_message_history=True)
async def purge(ctx, limit: int = 100, user: d.Member = None, *, matches: str = None):
    """Purge all messages, optionally from ``user``
    or contains ``matches``."""
    logger.info('purge', extra={'ctx': ctx})
    def check_msg(msg):
        if msg.id == ctx.message.id:
            return True
        if user is not None:
            if msg.author.id != user.id:
                return False
        if matches is not None:
            if matches not in msg.content:
                return False
        return True
    deleted = await ctx.channel.purge(limit=limit, check=check_msg)
    msg = await ctx.send(i18n(ctx, 'purge', len(deleted)))
    await a.sleep(2)
    await msg.delete()

即删除消息的命令

【讨论】:

  • 这是新的discord.py-rewrite 分支。上面问题中的代码来自较旧的异步分支(注意 send_message 超过 send 等)
猜你喜欢
  • 2020-08-24
  • 2019-04-24
  • 2019-09-16
  • 1970-01-01
  • 2018-11-07
  • 2021-08-24
  • 2018-05-02
  • 2020-07-04
  • 2020-08-25
相关资源
最近更新 更多