与 mcstatus 库结合使用时 CTX 命令不起作用
CTX commands not working when combined with the mcstatus library
我制作了一个 Discord 机器人,可以告诉您 Minecraft 的服务器状态!
它在带有 print
函数的控制台上工作正常,但它不能作为命令工作!机器人只是没有回应。我也在没有 mcstatus
库的情况下对其进行了测试,并且该命令有效。 (当然,没有它应该做什么。)
这是我的代码:
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")
有什么想法吗?
我认为该命令不起作用,因为您尚未在代码中声明您需要的意图。 Discord 机器人现在需要声明它们需要访问哪些事件,例如阅读消息。您可以在 discord.py 文档 here.
中阅读有关意图的信息
我还将“在线玩家”部分移到了一个变量中,并打印了它,这样你的命令就可以工作了。
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")
我认为这是因为您需要在 Discord 机器人中启用意图,并通过添加以下内容在代码中声明它们:
intents = discord.Intents.default()
并且还将 , intents=intents
放在 space 中,您声明机器人的前缀。
这也是我刚刚制作的一个机器人示例,也可以帮助执行一些命令。
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)
正如您在此处看到的,以下代码对我来说工作正常。
下图显示了命令的工作原理:command image
我制作了一个 Discord 机器人,可以告诉您 Minecraft 的服务器状态!
它在带有 print
函数的控制台上工作正常,但它不能作为命令工作!机器人只是没有回应。我也在没有 mcstatus
库的情况下对其进行了测试,并且该命令有效。 (当然,没有它应该做什么。)
这是我的代码:
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")
有什么想法吗?
我认为该命令不起作用,因为您尚未在代码中声明您需要的意图。 Discord 机器人现在需要声明它们需要访问哪些事件,例如阅读消息。您可以在 discord.py 文档 here.
中阅读有关意图的信息我还将“在线玩家”部分移到了一个变量中,并打印了它,这样你的命令就可以工作了。
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")
我认为这是因为您需要在 Discord 机器人中启用意图,并通过添加以下内容在代码中声明它们:
intents = discord.Intents.default()
并且还将 , intents=intents
放在 space 中,您声明机器人的前缀。
这也是我刚刚制作的一个机器人示例,也可以帮助执行一些命令。
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)
正如您在此处看到的,以下代码对我来说工作正常。
下图显示了命令的工作原理:command image