python-3.x ctx.channel 清除时,www.example.com具有交互模块的类型对象“MISSING”(不一致)

uqjltbpv  于 2022-12-15  发布在  Python
关注(0)|答案(1)|浏览(123)

我试着用一个斜杠命令来清除消息。但是,我得到了一个AttributeError: type object 'MISSING' has no attribute 'purge'错误。有人能帮我吗?下面是代码

# Les imports
import interactions
import os
import asyncio as a
from discord.ext import commands

# Le bot
slash = interactions.Client(token=token)

# Début du code
@slash.command(
    name="delete", 
    description="Supprime les X derniers messages",
    options = [
        interactions.Option(
            name="number_of_messages",
            description="Combien de messages à delete ?",
            type=interactions.OptionType.STRING,
            required=True,
        ),
    ],
    )
async def delete(ctx: interactions.CommandContext, number_of_messages: str):
    await ctx.channel.purge(int(number_of_messages))

slash.start()
wpcxdonn

wpcxdonn1#

discord.py v2中你不再需要discord-interaction库了。已经有库可以做了。所以你可以只使用discord.py

import discord
from discord.ext import commands
from discord import app_commands

class your_class_name(commands.Cog):
    def __init__(self, client:discord.Client):
        self.client = client

    @app_commands.command(name='purge', description="Delete a number of messages from a channel")
    @app_commands.checks.has_permissions(manage_messages=True)
    async def purge_message(self, interaction:discord.Interaction, number:int):
        //this way same as yours, but just using discord.py v2
        await interaction.channel.purge(limit=number)
        await interaction.response.send_message(f"{number} messages have been purged", ephemeral=True)

确保要使用此清除命令的用户具有manage_messages权限

相关问题