ctx.channel has type object "MISSING" with interactions module when trying to purge (Discord)

155 Views Asked by At

I tried making a command to purge messages with a slash command. However, I get a AttributeError: type object 'MISSING' has no attribute 'purge' error. Can anyone help me please? Here's the code

# 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()
1

There are 1 best solutions below

1
AudioBubble On

You don't need discord-interaction library anymore in discord.py v2. There already library to do it. So you can just using 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)

Make sure the one who want to use this purge command has manage_messages perms