【问题标题】:How do you make a Discord bot delete the message and it says what you said after the prefix like ".say <text>" in Python?如何让 Discord 机器人删除消息,并在 Python 中像“.say <text>”这样的前缀后说出你所说的内容?
【发布时间】:2018-05-19 01:24:05
【问题描述】:

我只是想知道,您如何使用“.say”之类的命令让 Discord 机器人说出某些内容,它会删除消息,然后说出“”部分。你如何在 Python 中做到这一点?我知道如何在 JavaScript 中执行此操作,这是我在 Discord.JS 中使用的代码:

const args = message.content.split(" ").slice(1);
if(message.content.startsWith(prefix + 'say')) {
    message.delete()
    var saytext = args.join(" ");
    message.channel.send(saytext)
    fs.appendFile("fbkbotlog.txt", `\n${ts} ${message.author.username} in ${message.guild.id} or ${message.guild.name} used say with text " ${saytext} "`)
}

我想知道如何在 discord.py 中做到这一点。我对 Discord.PY 甚至 Python 都不是很熟悉。

注意:我正在尝试使用 Just-Some-Bots MusicBot 代码

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    基本上是一样的:

    使用消息事件监听器(与您的 JS 示例非常相似)

    import discord
    from discord.ext import commands
    
    bot = discord.Client()
    prefix = "/"
    
    @bot.event
    async def on_ready():
        print("Online")
    
    @bot.event
    async def on_message(message):
        args = message.content.split(" ")[1:]
        if message.content.startswith(prefix + "say"):
            await bot.delete_message(message)
            await bot.send_message(message.channel, " ".join(args))
    
    bot.run("token")
    

    使用内置命令处理程序

    import discord
    from discord.ext import commands
    
    prefix = "/"
    bot = commands.Bot(prefix)
    
    @bot.event
    async def on_ready():
        print("Online")
    
    @bot.command(pass_context = True)
    async def say(ctx, *, mg = None):
        await bot.delete_message(ctx.message)
    
        if not mg: await bot.say("Please specify a message to send")
        else: await bot.say(mg)
    
    bot.run("token")
    

    【讨论】:

    • 没用。我正在尝试为命令编辑 Just-Some-Bots MusicBot 代码:P 我的朋友想要在该代码上使用 say 命令
    • 什么不起作用? o.o.为我工作。你的错误信息是什么? @IndoHQBlu
    • 它什么也没说它只是......根本不起作用......再说一遍,这是Just-Some-Bots MusicBot code
    【解决方案2】:
    import discord.ext
    
    bot = commands.Bot(";")
    
    @bot.event 
    async def on_message(message):
        if message.content.upper().startswith(";SAY"):
            args = message.content.split(" ")
            await bot.send_message(message.channel, "%s" % (" ".join(args[1:])))
            await bot.delete_message(message)
    

    【讨论】:

    • 您应该解释这与现有响应有何不同(或更好)。
    猜你喜欢
    • 2020-08-30
    • 2020-09-12
    • 1970-01-01
    • 1970-01-01
    • 2019-08-03
    • 2020-08-24
    • 2019-02-20
    • 2017-09-14
    • 2020-06-20
    相关资源
    最近更新 更多