【问题标题】:Discord.py Repeat message, delete commandDiscord.py 重复消息,删除命令
【发布时间】:2021-07-31 18:20:21
【问题描述】:

我已经得到它来重复消息,但是我如何让它删除命令前缀,并删除消息?

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'))

【问题讨论】:

    标签: discord.py


    【解决方案1】:

    要摆脱机器人发送命令前缀,您可以在发送时使用[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 文档

    【讨论】:

    • 唯一的问题是对每条消息都这样做,我希望它只在我想要的时候才这样做,因此为什么要使用命令。
    • @EvanSmith 我现在明白你的问题我已经相应地编辑了我的答案。这是你现在想要的吗?
    【解决方案2】:

    我建议使用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

    【讨论】:

      猜你喜欢
      • 2021-09-26
      • 1970-01-01
      • 2021-05-29
      • 2018-06-29
      • 2021-09-08
      • 2018-08-26
      • 2021-02-08
      • 2021-03-17
      • 1970-01-01
      相关资源
      最近更新 更多