首页 文章

你如何让Discord机器人删除该消息,并在Python中说出你在前缀之后说的“.say <text>”?

提问于
浏览
0

我只是想知道,你是如何让一个Discord机器人用“.say”这样的命令说出来的东西它删除了消息,然后说出了“”部分 . 你是如何用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代码

2 回答

  • 1

    基本上是一样的:

    Using the message event listener (非常类似于你的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")
    

    Using the in-built command handler

    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")
    
  • 0
    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)
    

相关问题