我想学习如何制作一个机器人,显示机器人正在运行的虚拟服务器的内存使用情况等,并使用从 discord 发送的命令。
答案 0 :(得分:0)
您可以使用 psutil 创建这样的命令。
这样的事情应该可以工作(未经测试):
import discord
from discord.ext import commands
import psutil
bot = commands.Bot(command_prefix='!')
@bot.command()
async def stats(ctx):
bedem = discord.Embed(title = 'System Resource Usage', description = 'See CPU and memory usage of the system.')
bedem.add_field(name = 'CPU Usage', value = f'{psutil.cpu_percent()}%', inline = False)
bedem.add_field(name = 'Memory Usage', value = f'{psutil.virtual_memory().percent}%', inline = False)
bedem.add_field(name = 'Available Memory', value = f'{psutil.virtual_memory().available * 100 / psutil.virtual_memory().total}%', inline = False)
await ctx.send(embed = bedem)
bot.run('token')
显然这是非常基础的,您可以使用 psutil 做更多事情,我建议您阅读他们的 documentation。