Discord.py 重复消息,删除命令

时间:2021-07-31 01:08:25

标签: discord.py

我已经让它一共重复了这条消息,但是我如何让它删除命令前缀并删除消息?

import discord
import os

client = discord.Client()

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
    if message.author == client.user:
        return
  
    if message.content.startswith('.say'):
        await message.channel.send(message.content)

client.run(os.getenv('TOKEN'))

2 个答案:

答案 0 :(得分:0)

要摆脱机器人发送命令前缀,您可以在发送时使用 [4:],这样就不会发送前四个字符!

要删除用户的消息,您可以使用以下命令:await message.delete()

所以完整的代码是:

import discord
import os

client = discord.Client()

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
    if message.author == client.user:
        return
  
    if message.content.startswith('.say'):
        await message.channel.send(message.content[4:])
    await message.delete()

client.run(os.getenv('TOKEN'))

我会查看链接 here

的 discord.py 文档

答案 1 :(得分:0)

我建议使用 discord.ext.commands 此处是将您的代码制成 commands.Bot 而不是 discord.client

使用 .say LONGMESSAGE HERE 调用命令

import discord
from discord.ext import commands
import os

bot = commands.Bot(command_prefix='.')

@bot.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@bot.command()
async def say(ctx,* , data:str = None):
    if not data:
        return await ctx.send('please enter some data')

    await ctx.message.delete()
    await ctx.send(data)

bot.run(os.getenv('TOKEN'))

Read more