你如何让Discord机器人删除这条消息,并在前缀之后说出你所说的内容,例如&#34; .say <text>&#34;在Python中?

时间:2017-12-05 11:36:54

标签: python discord discord.py

我只是想知道,你是如何使用像#34; .say&#34;这样的命令让Discord机器人说些什么。它会删除该消息,然后说出&#34;&#34;部分。你是如何用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 个答案:

答案 0 :(得分: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")

答案 1 :(得分: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)