Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >有人能解释一下我在这个discord.py程序中犯的错误吗?如何在我的Python discord bot代码中找到错误?

有人能解释一下我在这个discord.py程序中犯的错误吗?如何在我的Python discord bot代码中找到错误?
EN

Stack Overflow用户
提问于 2021-08-31 18:29:47
回答 1查看 103关注 0票数 0
代码语言:javascript
运行
AI代码解释
复制
#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))

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

代码语言:javascript
运行
AI代码解释
复制
# 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'))

下面是错误:

代码语言:javascript
运行
AI代码解释
复制
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'
EN

回答 1

Stack Overflow用户

发布于 2021-09-01 02:27:32

你的错误来自这里:

代码语言:javascript
运行
AI代码解释
复制
from main.py import (addition, substraction, multiplication, division, squarePower, cubePower, numPower, squareRoot, cubeRoot, numRoot, randomNum, pythagoras, decdeg2dms, trigono, status_swap)

您只需要删除.py

代码语言:javascript
运行
AI代码解释
复制
from main import (addition, substraction, multiplication, division, squarePower, cubePower, numPower, squareRoot, cubeRoot, numRoot, randomNum, pythagoras, decdeg2dms, trigono, status_swap)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69007162

复制
相关文章
offset size_c语言sizeof求结构体长度
sizeof与offsetof在程序中经常遇到,但在面试中其应用使得许多小伙伴吃闭门羹,被面试官问得哑口无言。接下来对两者的应用做详细介绍。
全栈程序员站长
2022/09/23
7080
RSA密钥长度、明文长度和密文长度
本文介绍RSA加解密中必须考虑到的密钥长度、明文长度和密文长度问题,对第一次接触RSA的开发人员来说,RSA算是比较复杂的算法,天缘以后还会补充几篇RSA基础知识专题文章,用最简单最通俗的语言描述RSA,让各位了解RSA算法本身其实也很简单,RSA的复杂度是因为数学家把效率和安全也考虑进去的缘故。
竹清
2018/08/31
22.5K0
Redis的SDS的内部结构,它的长度和空间预分配策略
SDS(Simple Dynamic String)是Redis中用于处理字符串的数据结构。
一凡sir
2023/09/14
5050
Redis的SDS的内部结构,它的长度和空间预分配策略
NUMBER长度的误解
NUMBER类型是Oracle的一种变长数值类型,他的取值范围是10^(-130)-10^126(不包括),精度是38位,存储空间是1-22字节。
bisal
2020/04/17
3.2K0
数据结构--链表--单链表中环的检测,环的入口,环的长度的计算
完整代码见:https://github.com/hitskyer/course/tree/master/dataAlgorithm/chenmingming/linkedList
Michael阿明
2021/02/20
5950
数据结构--链表--单链表中环的检测,环的入口,环的长度的计算
24:单词的长度
24:单词的长度 总时间限制: 1000ms 内存限制: 65536kB描述 输入一行单词序列,相邻单词之间由1个或多个空格间隔,请对应地计算各个单词的长度。  注意,如果有标点符号(如连字符,逗号),标点符号算作与之相连的词的一部分。没有被空格间开的符号串,都算作单词。 输入一行单词序列,最少1个单词,最多300个单词,单词之间用至少1个空格间隔。单词序列总长度不超过1000。输出依次输出对应单词的长度,之间以逗号间隔。样例输入 She was born in 1990-01-02 and
attack
2018/04/03
1.8K0
webservice最大长度_网址最大长度
HTTP GET请求的最大长度是多少? 是否定义了一个响应错误,如果服务器收到超过此长度的GET请求,服务器可以/应该返回该错误?
全栈程序员站长
2022/09/23
3.3K0
webservice最大长度_网址最大长度
套接字地址结构的长度之类的值-结果参数要用指针来传递原因
当函数调用时,结构大小是一个值,它告诉内核该结构大小,这样内核在写该结构时,不至于越界;当函数返回时,结构大小又是一个结果,他告诉进程内核在改结构中究竟 存储了多少信息。
心跳包
2020/08/31
1K0
LeetCode 数据结构与算法无重复字符的最长子串长度详解
我们可以利用 hash 来判断不重复子串。如果 hash 表中存在该字符串,就进入下次循环;如果不存在,就放入集合中然后在再移动右指针。
PHP开发工程师
2022/04/12
2770
mysql前缀索引 默认长度_如何确定前缀索引的长度?
Specified key ‘uniq_code’ was too long; max key length is 767 bytes.
全栈程序员站长
2022/08/31
3.7K0
密码学系列之:Merkle–Damgård结构和长度延展攻击
Merkle–Damgård结构简称为MD结构,主要用在hash算法中抵御碰撞攻击。这个结构是一些优秀的hash算法,比如MD5,SHA-1和SHA-2的基础。今天给大家讲解一下这个MD结构和对他进行的长度延展攻击。
程序那些事
2021/07/23
1.9K0
数据结构算法操作试题(C++/Python)——最后一个单词的长度
数据结构算法操作试题(C++/Python):数据结构算法操作试题(C++/Python)——目录
莫斯
2020/09/09
3200
ACL 2022 | 基于长度感知注意机制的长度可控摘要模型
以往的长度可控摘要模型大多在解码阶段控制长度,而编码阶段对指定的摘要长度不敏感。这样模型倾向于生成和训练数据一样长的摘要。在这篇论文中,作者提出了一种长度感知注意机制(LAAM,length-aware attention mechanism)来适应基于期望长度的编码。
zenRRan
2023/01/12
9910
ACL 2022 | 基于长度感知注意机制的长度可控摘要模型
任务显示与隐藏,任务管理器中应用程序显示与隐藏,悬浮窗任务栏显示 效果
1. 隐藏底部 “任务栏图标”和 隐藏 “任务任务管理器——应用程序—任务” ModifyStyleEx(WS_EX_APPWINDOW,WS_EX_TOOLWINDOW);                        
全栈程序员站长
2021/12/06
1.6K0
序列比对的长度限制
前几天做序列比对,试了MUCSLE和MAFFT,但是程序总是被kill。刚开始以为是序列格式不对,但是检查到最后发现是序列太长了。以前没注意过这些比对算法对长度的要求,此文记录一下。
Listenlii-生物信息知识分享
2020/05/29
4K0
QML获取文字的长度
额外增加一层的Text, 并通过绑定父级的Text文本属性,即使父级的文本的width属性改变也不会影响到textLength的获取。
Qt君
2019/07/15
3.3K0
javascript计算对象的长度
计算对象的长度,即获取对象属性的个数 方法一:通过for in 遍历对象,并通过hasOwnProperty判断是否是对象自身可枚举的属性 var obj = {"c1":1,"c2":2}; function countProperties(obj){ for(var property in obj){ if(Object.prototype.hasOwnProperty.call(obj,property){ count++; })
用户1214487
2018/01/24
1.6K0
长度未知的数组大小
malloc 和 calloc都可以被用于申请堆上的空间。 malloc 和 calloc主要有两点不同:
小飞侠xp
2021/04/13
2.6K0
移动适配的长度单位
例:html中字体尺寸为 20px,盒子宽度为 5rem,则最后显示的盒子宽度为 100px。
岳泽以
2022/10/26
1.3K0
移动适配的长度单位
点击加载更多

相似问题

在Blender中获取关键帧点数组

14

blender _ python中的连接节点

12

命名组+x修改器

11

收集选项组值并将值插入到表中

12

如何使用Python在Blender 2.78a中制作关键帧?

12
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档