谁能解释一下我制作的discord.py程序中的错误吗?如何在Python discord bot代码中找到错误?

2024-04-16 22:44:26 发布

您现在位置:Python中文网/ 问答频道 /正文

#This is the cog file in the name : "example.py"
import discord
import os
import math
from discord.ext import commands, tasks
import random
from online import keep_alive
from itertools import cycle
from fractions import Fraction
from discord_slash import SlashCommand
from main.py import (addition, substraction, multiplication, division, squarePower, cubePower, numPower, squareRoot, cubeRoot, numRoot, randomNum, pythagoras, decdeg2dms, trigono, status_swap)

class codeUpdate(commands.Cog) :
  def _init_ (self, client) :
    self.client = client
    self.status = cycle([
  " Unanswered Question of Life", 
  " Self - Referential Paradox", 
  " Near-infinite density?", 
  " Dark matter ?", 
  " Measurement of the speed of light in one straight line", 
  " Schrodinger's cat ???"
  "436c69636b2074686973206c696e6b20666f72206672656520766275636b7320212121212121203a200a68747470733a2f2f7777772e796f75747562652e636f6d2f77617463683f763d6451773477395767586351 (HEXDEC)",
  "The light side of Discord is the path of many abilities that some considered unnatural",
  "Who killed Jeffrey Epstein?",
  "One day, everybody will die and the universe collapse. {Existential Crisis Intensifies(┬┬﹏┬┬)}","Stop procastinating","Go touch some grass"])

  @ tasks.loop(seconds = 300)
  async def status_swap(self):
    await self.client.change_presence(activity = discord.Game(next(self.status)))

  @ commands.Cog.listener()
  async def on_ready(self):
    print("We have logged in {0.user}".format(self.client))
    self.status_swap.start()

  @ commands.command(name='mthadd', aliases=['plus'])
  async def mthadd(self, ctx, x: float, y : float) :
    response = (f"The result of addition between {x} and {y} is {addition(x, y)}.")
    await ctx.send(response)

  @ commands.command(name='mthsubs', aliases=['minus'])
  async def mthsubs(self, ctx, x: float, y : float) :
    response = (f"The result of substraction between {x} and {y} is {substraction(x, y)}.")
    await ctx.send(response)

  @ commands.command(name='mthmulti', aliases=['times'])
  async def mthmulti(self, ctx, x: float, y : float) :
    response = (f"The result of multiplication between {x} and {y} is {multiplication(x, y)}.")
    await ctx.send(response)

  @ commands.command(name='mthdiv', aliases=['divide'])
  async def mthdiv(self, ctx, x: float, y : float) :
    response = (f"The result of division between {x} and {y} is {division(x, y)}.")
    await ctx.send(response)

  @ commands.command(name='mthsquare', aliases=['square'])
  async def mthsquare(self, ctx, x: float) :
    response = (f"The result of {x} squared is {squarePower(x)}.")
    await ctx.send(response)

  @ commands.command(name='mthcube', aliases=['cube'])
  async def mthcube(self, ctx, x: float) :
    response = (f"The result of {x} cubed is {cubePower(x)}.")
    await ctx.send(response)

  @ commands.command(name='mthpower', aliases=['varpower'])
  async def mthpower(self, ctx, x: float, y : float) :
    response = (f"The result of {x} to the power of {y} is {numPower(x, y)}.")
    await ctx.send(response)

  @ commands.command(name='mthsqrt', aliases=['squareroot'])
  async def mthsqrt(self, ctx, x: float) :
    response = (f"The result of square root of {x} is {squareRoot(x)}.")
    await ctx.send(response)

  @ commands.command(name='mthcbrt', aliases=['cuberoot'])
  async def mthcbrt(self, ctx, x: float) :
    response = (f"The result of cube root of {x} is {cubeRoot(x)}.")
    await ctx.send(response)

  @ commands.command(name='mthnumrt', aliases=['varroot'])
  async def mthnumrt(self,ctx, x: float, y : float) :
    response = (f"The result of {x} root {y} is {numRoot(x, y)}.")
    await ctx.send(response)

  @ commands.command(name='mthgenerator', aliases=['generator'])
  async def mthgenerator(self, ctx, x: float, y : float) :
    response = (f"The randomised number between {x} and {y} is {randomNum(x, y)}.")
    await ctx.send(response)

  @ commands.command(name='mthpytha', aliases=['pythagorean'])
  async def mthpytha(self, ctx, x : float, y : float, z : int) :
    if z == 1 :
      response = (f"The hypotenuse of side {x} and side {y} is {pythagoras(x, y, z)}.")
      await ctx.send(response)
    elif z == 2 : 
      response = (f"The opposite side of side {x} and side {y} is {pythagoras(x, y, z)}.")
      await ctx.send(response)
    elif z == 3 : 
      response = (f"The adjacent side of side {x} and side {y} is {pythagoras(x, y, z)}.")
      await ctx.send(response)

  @ commands.command(name='mthdegminsec', aliases=['degminsec'])
  async def mthdegminsec(self, ctx, x : float): 
    response = (f"Once converted, {x} becomes {decdeg2dms(x)} in the form of degrees and minutes.")
    await ctx.send(response)

  @ commands.command(name='mthtrigoratio', aliases=['sincostan'])
  async def mthtrigoratio(self, ctx, x : float, y : float, z : int) :
    if z == 1 :
      response = (f"The sine for opposite side {x} and hypotenuse {y} is {trigono(x, y, z)}.")
      await ctx.send(response)
    elif  z == 2 :
      response = (f"The cosine for adjacent side {x} and hypotenuse {y} is {trigono(x, y, z)}.")
      await ctx.send(response)
    elif z == 3 :
      response = (f"The tangent for opposite side {x} and adjacent side {y} is {trigono(x, y, z)}.")
      await ctx.send(response)

def setup(client : commands.client) :
  client.add_cog(codeUpdate(client))

因此,我尝试在我的bot中实现cogs,以便在不中断bot的情况下更新bot,但当我在实现bot后测试bot时,bot停止工作。我看过很多YouTube教程和文档,但我看不懂。我知道主文件中的一行代码有问题。如果有人知道如何解决这个问题,请帮助我。提前谢谢

# This is my main file for my discord bot
import discord
import os
import math
from discord.ext import commands, tasks
import random
from online import keep_alive
from itertools import cycle
from fractions import Fraction
from discord_slash import SlashCommand

client = commands.Bot(command_prefix = "!",help_command = None)

slash = SlashCommand(client, sync_commands = True)

def addition(x : float, y : float) :
  return x + y

def substraction(x : float, y : float) :
  return x - y

def multiplication(x : float, y : float) :
  return x * y

def division(x : float, y : float) :
  return x / y

def squarePower(x : float) :
  return x ** 2

def cubePower(x : float) :
  return x ** 3

def numPower(x : float, y : float) :
  return x ** y

def squareRoot(x : float) :
  return math.sqrt(x)

def cubeRoot(x : float) :
  return x ** (1./3.)

def numRoot(x : float, y : float) :
  return x ** (1./y) 

def randomNum(x : int, y : int) :
  return random.randint(x,y)

def pythagoras(x : float, y : float, z : int):
  if z == 1 :
    return squareRoot(squarePower(x) + squarePower(y))
  elif z == 2 :
    if x > y :
      return squareRoot(squarePower(x) - squarePower(y))
    else :
      return squareRoot(squarePower(y) - squarePower(x))
  elif z == 3 :
    if x > y :
      return squareRoot(squarePower(x) - squarePower(y))
    else :
      return squareRoot(squarePower(y) - squarePower(x))

def decdeg2dms(x : float):
  negative = x < 0
  x = abs(x)
  minutes,seconds = divmod(x*3600,60)
  degrees,minutes = divmod(minutes,60)
  if negative:
    if degrees > 0:
      degrees = -degrees
  elif minutes > 0:
      minutes = -minutes
  else:
      seconds = -seconds
  return (degrees,minutes,seconds)

def trigono(x : float, y : float, z : int) :
  return Fraction(x/y).limit_denominator()

@ slash.slash(description = "Add two numbers")
async def add(ctx, x: float, y : float) :
  response = (f"The result of addition between {x} and {y} is {addition(x, y)}.")
  await ctx.send(response)

@ slash.slash(description = "Substract two numbers")
async def subs(ctx, x: float, y : float) :
  response = (f"The result of substraction between {x} and {y} is {substraction(x, y)}.")
  await ctx.send(response)

@ slash.slash(description = "Multiply two numbers")
async def multi(ctx, x: float, y : float) :
  response = (f"The result of multiplication between {x} and {y} is {multiplication(x, y)}.")
  await ctx.send(response)

@ slash.slash(description = "Divide two numbers")
async def div(ctx, x: float, y : float) :
  response = (f"The result of division between {x} and {y} is {division(x, y)}.")
  await ctx.send(response)

@ slash.slash(description = "Squared a number")
async def square(ctx, x: float) :
  response = (f"The result of {x} squared is {squarePower(x)}.")
  await ctx.send(response)

@ slash.slash(description = "Cubed a number")
async def cube(ctx, x: float) :
  response = (f"The result of {x} cubed is {cubePower(x)}.")
  await ctx.send(response)

@ slash.slash(description = "Power a number to another number")
async def varpower(ctx, x: float, y : float) :
  response = (f"The result of {x} to the power of {y} is {numPower(x, y)}.")
  await ctx.send(response)

@ slash.slash(description = "Square root a number")
async def squareroot(ctx, x: float) :
  response = (f"The result of square root of {x} is {squareRoot(x)}.")
  await ctx.send(response)

@ slash.slash(description = "Cube root a number")
async def cuberoot(ctx, x: float) :
  response = (f"The result of cube root of {x} is {cubeRoot(x)}.")
  await ctx.send(response)

@ slash.slash(description = "Root a number to another number")
async def varroot(ctx, x: float, y : float) :
  response = (f"The result of {x} root {y} is {numRoot(x, y)}.")
  await ctx.send(response)

@ slash.slash(description = "Produce a randomise number between two numbers")
async def generator(ctx, x: float, y : float) :
  response = (f"The randomised number between {x} and {y} is {randomNum(x, y)}.")
  await ctx.send(response)

@ slash.slash(description = "z(1): Hypotenuse. z(2) : Opposite side. z(3) : Adjacent side.")
async def pytha(ctx, x : float, y : float, z : int) :
  if z == 1 :
    response = (f"The hypotenuse of side {x} and side {y} is {pythagoras(x, y, z)}.")
    await ctx.send(response)
  elif z == 2 : 
    response = (f"The opposite side of side {x} and side {y} is {pythagoras(x, y, z)}.")
    await ctx.send(response)
  elif z == 3 : 
    response = (f"The adjacent side of side {x} and side {y} is {pythagoras(x, y, z)}.")
    await ctx.send(response)

@ slash.slash(description = "Convert an angular degree value to degree, minutes and second.")
async def degminsec(ctx, x : float): 
  response = (f"Once converted, {x} becomes {decdeg2dms(x)} in the form of degrees and minutes.")
  await ctx.send(response)

@ slash.slash(description = "z(1) : Sine. z(2) : Cosine. z(3) : Tangent.")
async def trigoratio(ctx, x : float, y : float, z : int) :
  if z == 1 :
    response = (f"The sine for opposite side {x} and hypotenuse {y} is {trigono(x, y, z)}.")
    await ctx.send(response)
  elif  z == 2 :
    response = (f"The cosine for adjacent side {x} and hypotenuse {y} is {trigono(x, y, z)}.")
    await ctx.send(response)
  elif z == 3 :
    response = (f"The tangent for opposite side {x} and adjacent side {y} is {trigono(x, y, z)}.")
    await ctx.send(response)

@ client.command()
async def loadCode(ctx, extension) :
  client.load_extension(f"cogs.{extension}")

@ client.command()
async def unloadCode(ctx, extension) :
  client.unload_extension(f"cogs.{extension}")

for filename in os.listdir("./cogs") :
  if filename.endswith(".py") :
    client.load_extension("cogs." + filename[:-3])

keep_alive()
client.run(os.getenv('MATH_VAR'))

以下是错误:

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 606, in _load_from_module_spec
    spec.loader.exec_module(lib)
  File "<frozen importlib._bootstrap_external>", line 848, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/runner/Math-Bot/cogs/example.py", line 10, in <module>
    from main.py import (addition, substraction, multiplication, division, squarePower, cubePower, numPower, squareRoot, cubeRoot, numRoot, randomNum, pythagoras, decdeg2dms, trigono, status_swap)
ModuleNotFoundError: No module named 'main.py'; 'main' is not a package

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 606, in _load_from_module_spec
    spec.loader.exec_module(lib)
  File "<frozen importlib._bootstrap_external>", line 848, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/runner/Math-Bot/cogs/example.py", line 10, in <module>
    from main.py import (addition, substraction, multiplication, division, squarePower, cubePower, numPower, squareRoot, cubeRoot, numRoot, randomNum, pythagoras, decdeg2dms, trigono, status_swap)
  File "/home/runner/Math-Bot/main.py", line 173, in <module>
    client.load_extension("cogs." + filename[:-3])
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 678, in load_extension
    self._load_from_module_spec(spec, name)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 609, in _load_from_module_spec
    raise errors.ExtensionFailed(key, e) from e
discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.example' raised an error: ModuleNotFoundError: No module named 'main.py'; 'main' is not a package

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 173, in <module>
    client.load_extension("cogs." + filename[:-3])
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 678, in load_extension
    self._load_from_module_spec(spec, name)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 608, in _load_from_module_spec
    del sys.modules[key]
KeyError: 'cogs.example'

Tags: andoftheselfsendasyncisresponse
1条回答
网友
1楼 · 发布于 2024-04-16 22:44:26

您的错误来自这里:

from main.py import (addition, substraction, multiplication, division, squarePower, cubePower, numPower, squareRoot, cubeRoot, numRoot, randomNum, pythagoras, decdeg2dms, trigono, status_swap)

您只需要删除.py

from main import (addition, substraction, multiplication, division, squarePower, cubePower, numPower, squareRoot, cubeRoot, numRoot, randomNum, pythagoras, decdeg2dms, trigono, status_swap)

相关问题 更多 >