0
votes

Discord.py - Autoriser la commande dans un canal spécifique

je dois utiliser ma commande discord dans un canal spécifique, j'utilise @ client.command () je veux dire que si j'ai utilisé ma commande dans un autre canal ne fonctionnera pas

@client.command()
async def w(ctx):
    channel = client.get_channel(775491463940669480)

    await ctx.send("hello) ```

 


5 commentaires

Vous ne voulez utiliser cette commande que sur un canal spécifique, cela ne fonctionnera pas sur les autres, non?


oui exactement, comment je peux faire cela avec @ client.command ... etc


Je n'ai rien trouvé à ce sujet dans la documentation Discord, je sais que ça existe bien sûr lol !. Mais je ne l'ai pas trouvé :(


ctx.channel est un objetdiscord.channel.TextChannel et possède les attributs associés tels que name et id - est-ce que cela vous donne ce dont vous avez besoin?


Je voulais que ma commande soit autorisée sur un canal spécifique, de toute façon triée, je l'ai finalement trouvée lool. J'ai posté la réponse. idk si cette méthode noob ou pas de loll: D mais de toute façon cela a rempli mon objectif


3 Réponses :


0
votes
client = commands.Bot(command_prefix="!")

@client.event
async def on_ready():
    print("bot is Online")
-
@client.event
async def on_message(message):
    cmdChannel = client.get_channel(776179059355942943)
    if message.content.lower().startswith('!w'):
        if message.channel.id == cmdChannel.id:
            #command invoked in command channel - execute it
            await client.process_commands(message)
        else:
            #command attempted in non command channel - redirect user
            await message.channel.send('Write this command in {}'.format(cmdChannel.mention))

@client.command()
async def w(ctx, amount):
    await ctx.send(f"done {amount}")

0 commentaires

0
votes

Vous pouvez vérifier si ctx.channel est égal à votre chaîne spécifique.

@client.command()
async def w(ctx):
    channel = client.get_channel(775491463940669480)
    if channel == ctx.channel:
        await ctx.send("hello)


0 commentaires

0
votes

Il existe même un moyen plus simple de le faire sans obtenir la chaîne. Et sans avoir à indenter tout le code.

@client.command()
async def w(ctx):
    # exit command if not the desired channel.
    if ctx.channel.id is not 775491463940669480:
        return
    
    #code here
    await ctx.send("hello")


0 commentaires