Search code examples
pythondiscorddiscord.pyminecraft

CTX commands not working when combined with the mcstatus library


I made a Discord bot that tells you a Minecraft's server status! It works fine on the console with the print function, but it doesn’t work as a command! The bot just doesn’t respond. Also I tested it without the mcstatus library and the command worked. (Without what it's supposed to do, of course.)

This is my code:

import discord
from discord.ext import commands
from mcstatus import JavaServer

client = commands.Bot(command_prefix = "!")
client.remove_command("help")

server = JavaServer.lookup("mc.elitesmp.co:25588")
status = server.status()

@client.event
async def on_ready():
    print('Logged in as: "' + str(client.user) + '"')
    print(f"Elite SMP has {status.players.online} players online!")

@client.command() # <--- This is the command that doesn't work!
async def info(ctx):
    await ctx.send("test")

client.run("token")

Any ideas?


Solution

  • I think it’s because you need to enable intents in your Discord bot, and also declare them in the code by adding:

    intents = discord.Intents.default()

    And also putting , intents=intents in the space where you declare your bot's prefix.

    This is also an example of a bot I just made, to help with some command also.

    import discord
    from discord.ext import commands
    from mcstatus import JavaServer
    from mcstatus import BedrockServer
    
    
    intents = discord.Intents.default()
    
    bot = commands.Bot(command_prefix='?', intents=intents)
    
    TOKEN = "removed for obvious reasons"
    
    @bot.event
    async def on_ready():
        print('ready')
        await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching,name="Minecraft Servers"))
    
    @bot.event
    async def on_command_error(ctx, error):
        if isinstance(error,commands.CommandInvokeError):
            await ctx.reply("No such server was found")
    
    @bot.command()
    async def java(ctx, message):
        server = JavaServer.lookup(message)
        status = server.status()
        embed = discord.Embed(title=f"{message}", description=f"info on {message}")
        embed.add_field(name="Players Online :green_circle:", value=f"The server has {status.players.online} players online", inline=False)
        embed.add_field(name="Server Response :warning:", value=f"The server replied in {status.latency}ms", inline=False)
        await ctx.reply(embed=embed)
    
    @bot.command()
    async def bedrock(ctx, message):
        server = BedrockServer.lookup("message")
        status = server.status()
        embed = discord.Embed(title=f"{message}", description=f"info on {message}")
        embed.add_field(name="Players Online :green_circle:", value=f"The server has {status.players.online} players online", inline=False)
        embed.add_field(name="Server Response :warning:", value=f"The server replied in {status.latency}ms", inline=False)
        await ctx.reply(embed=embed)
    
    
    bot.run(TOKEN)
    

    The following code works fine for me, as you can see here.

    The following image shows how the commands work: command image