'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 1:[1]
I think the command isn't working, because you haven't declared what intents you need in code. Discord bots now need to declare what events they need to access, such as reading messages. You can read about intents in the discord.py documentation here.
I also moved the "players online" part to a variable, and printed that instead, and that got your command to work.
intents = discord.Intents.default()
client = commands.Bot(command_prefix = "!", intents = intents)
client.remove_command("help")
player_text = f"Elite SMP has {status.players.online} players online!"
@client.event
async def on_ready():
print(player_text)
@client.command()
async def test(ctx):
await ctx.send("test")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Peter Mortensen |
