如何在 discord.py 中创建可以打印线性方程图形的命令?
How can I make a command in discord.py that can print the graph of a linear equation?
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
您的代码中还有更多错误
plotlineareq
也应该把self
作为第一个参数
- 调用
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))
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
您的代码中还有更多错误
plotlineareq
也应该把self
作为第一个参数- 调用
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))