user17507616
user17507616

Reputation:

How do I use discord commands

I have tried making a bot in discord.py and i cant figure out how to use discord.ext.

The part that doesn't work is this:

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

@bot.command()
async def test(ctx, arg):
    await ctx.channel.send(arg)

When I type .test whatever in discord nothing happens

Is it because of the api update? If so what do I need to change

This is the entire code:

import os
import discord
import asyncio
from dotenv import load_dotenv
from discord.ext import commands
from discord.ext import bot


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

@bot.command()
async def test(ctx, arg):
    await ctx.channel.send(arg)

load_dotenv()

token = os.getenv("TOKEN")

client = discord.Client()


class MyClient(discord.Client):
    async def on_ready(self):
        print('Logged on as {0}!'.format(self.user))

    async def on_message(self, message):
        f = open("muteMsg.txt", "r")
        muteMsg = f.read()
        f.close()

        print('Message from {0.author}: {0.content}'.format(message))

        id = message.author.id
        if id == 0:
            await message.delete()
            if muteMsg == "1":
                await message.channel.send(f"stfu {message.author.mention}")

        elif message.content.startswith('good bot') or message.content.startswith('Good bot'):
            await message.channel.send(':)')

        elif message.content.startswith('bad bot') or message.content.startswith('Bad bot'):
            await message.channel.send(':(')

        elif message.content.startswith('.rickroll'):
            await message.channel.send('https://www.youtube.com/watch?v=dQw4w9WgXcQ')

        elif message.content.startswith('.help'):
            await message.channel.send('commands list:\n.help: shows this\n.rickroll: well youll have to try yourself\n')

        elif message.content.startswith('.togglemutemsgs'):
            if muteMsg == "0":
                f = open("muteMsg.txt", "w")
                f.write("1")
                f.close()
                await message.channel.send('mute messages on')
            elif muteMsg == "1":
                f = open("muteMsg.txt", "w")
                f.write("0")
                f.close()
                await message.channel.send('mute messages off')


client = MyClient()
client.run(token)

Upvotes: 0

Views: 666

Answers (3)

Nyghl
Nyghl

Reputation: 168

To use commands extension, you need to define a bot via commands.Bot because discord.Client doesn't have any infrastructure to make commands.

So you should change your inheritance in class MyClient, it should directly inherit from the commands.Bot. And you should pass the args/kwargs to your MyClient class, then you should run that classes instance rather than discord.Client.

Upvotes: 0

osego
osego

Reputation: 138

The core issue here is that while you do instantiate discord.ext.commands.Bot on line 9 you never actually use it, instead calling .run() on your subclass of discord.Client which does not interact with your defined method in any way.

The simplest way of getting your command to work is to change your last line from client.run(token) to

bot.run(token)

but your handlers of on_ready and on_message events will not work anymore - you should learn more to figure out why and how to solve it.

As a side note: you really shouldn't be putting any complex logic in on_message handler and you definitely should not be opening files there.

Upvotes: 1

Siddhant Chauhan
Siddhant Chauhan

Reputation: 222

Well, If you want to take input from user ,you need to use async def test(ctx, *, arg), Try using the code below:

@bot.command()
async def test(ctx, *,arg):
    await ctx.channel.send(arg)

And also beware of the command_prefix properly. Thank you :)

Upvotes: 0

Related Questions