python - 如何在 discord.py 中创建一个可以打印线性方程图的命令?
问题描述
import discord
from discord.ext import commands
import matplotlib.pyplot as plt
class Equations(commands.Cog):
def __init__(self, client):
self.client = client
def plotlineareq(a, b, clr):
x = [-10, 10]
y = [(a * i + b) for i in x]
plt.figure(figsize=(7, 7)) # #Size of Graph
plt.xlim(x) # #X Range [-6,6]
plt.ylim(x) # #Y Range [-6,6]
axis = plt.gca() # #Get Current Axis
plt.plot(axis.get_xlim(), [0, 0], 'k--') # #X Axis Plots Line Across
plt.plot([0, 0], axis.get_ylim(), 'k--') # #Y Axis Plots Line Across
plt.locator_params(axis="x", nbins=20)
plt.locator_params(axis="y", nbins=20)
plt.plot(x, y, label='linear', linestyle='-', color=clr)
plt.ylabel('y')
plt.xlabel('x')
mm = str(a)
bb = str(b)
plt.title("y = " + mm + "x + " + bb)
plt.grid()
plt.savefig("foo.png")
@commands.command()
async def linear(self, message):
try:
msg = message.content
msg = msg.split("*linear")[1]
msg = msg.replace(" ", "")
mm = msg.split("x")[0]
mx = mm.replace("x", "").replace("y=", "")
bx = msg.split("+")[1]
Equations.plotlineareq(mx, bx, 'b')
file = discord.File("foo.png", filename='foo.png')
embed = discord.Embed(color=0xff0000)
embed = embed.set_image(url="attachment://foo.png")
await message.channel.send(file=file, embed=embed)
except:
message.channel.send("An error occurred")
def setup(client):
client.add_cog(Equations(client))
到目前为止,这是我的 cog 的代码。函数“plotlineareq()”首先制作一个有 4 个象限的图形。然后它接受变量“a”(即梯度)和变量“b”(即 y 截距)。它从这些变量创建一个图形并将其保存为 foo.png。这部分单独工作。另一个函数应该等待以我的前缀“*”和“线性”开头的消息。示例消息模板为“*linear y=3x+2”。发送消息时,它会从消息中获取值 a 和 b(在本例中为 a=3 和 b=2。这部分也可以单独正常工作,然后使用这些变量运行函数“plotlineareq()”,然后获取它创建的图像“foo”并将其作为嵌入发送。没有错误消息显示为输出。它似乎并没有触发“线性”功能。我做错了什么?
编辑:在接受建议后
@commands.command()
async def linear(self, ctx, equation):
try:
equation = equation.replace(" ", "")
mx = equation.split("x")[0]
mx = equation.replace("x", "").replace("y=", "")
bx = equation.split("+")[1]
Equations.plotlineareq(mx, bx, 'b')
file = discord.File("foo.png", filename='foo.png')
embed = discord.Embed(color=0xff0000)
embed = embed.set_image(url="attachment://foo.png")
await ctx.channel.send(file=file, embed=embed)
它仍然无法正常工作。再次没有错误消息。它只是没有触发该功能。
解决方案
message
您首先在命令中传递的参数是,Context
您还需要传递方程参数
@commands.command()
async def linear(self, ctx, equation): # The `equation` arg is going to be a string
print(equation)
# *linear y=3x+2
# [bot] y=3x+2
您的代码中有更多错误
- the
plotlineareq
也应该把 theself
作为第一个参数 - 调用
plotlineareq
函数时,不要这样调用,而是这样Equation.plotlineareq
调用:self.plotlineareq
您的整个代码已修复:
import discord
from discord.ext import commands
import matplotlib.pyplot as plt
class Equations(commands.Cog):
def __init__(self, client):
self.client = client
def plotlineareq(self, a, b, clr):
x = [-10, 10]
y = [(a * i + b) for i in x]
plt.figure(figsize=(7, 7)) # #Size of Graph
plt.xlim(x) # #X Range [-6,6]
plt.ylim(x) # #Y Range [-6,6]
axis = plt.gca() # #Get Current Axis
plt.plot(axis.get_xlim(), [0, 0], 'k--') # #X Axis Plots Line Across
plt.plot([0, 0], axis.get_ylim(), 'k--') # #Y Axis Plots Line Across
plt.locator_params(axis="x", nbins=20)
plt.locator_params(axis="y", nbins=20)
plt.plot(x, y, label='linear', linestyle='-', color=clr)
plt.ylabel('y')
plt.xlabel('x')
mm = str(a)
bb = str(b)
plt.title("y = " + mm + "x + " + bb)
plt.grid()
plt.savefig("foo.png")
@commands.command()
async def linear(self, ctx, equation):
try:
equation = equation.replace(" ", "")
mx = equation.split("x")[0]
mx = equation.replace("x", "").replace("y=", "")
bx = equation.split("+")[1]
self.plotlineareq(mx, bx, 'b')
file = discord.File("foo.png", filename='foo.png')
embed = discord.Embed(color=0xff0000)
embed = embed.set_image(url="attachment://foo.png")
await ctx.send(file=file, embed=embed)
except Exception as e:
await ctx.send(f"An error occured: {e}")
def setup(client):
client.add_cog(Equation(client))
推荐阅读
- json - bower 注册不将包源托管到 Nexus
- haskell - Haskell从列表列表中提取列表
- java - Java 突围游戏 - 移除砖块
- notifications - Flutter:即使我的应用已关闭,我如何才能收到本地通知?
- jquery - 如何在 mozilla add on 中使用 jquery 执行发布请求?
- java - 用 lambda 表达式替换 for-each 循环
- python - 如何进入下一个周期?
- android - 是否需要在通知生效之前将 APK 上传到 Google Play?
- angular - 试图让 Angular 2 在渲染之前等待数据加载
- verilog - Verilog 中的 10:1024 位解码器