当前位置: 首页 > 知识库问答 >
问题:

移动到新电脑后,Discord bot代码已损坏

左丘季
2023-03-14

因此,我有一个不和谐的机器人,我正在工作一段时间前,我终于有时间把它移动到我的新电脑,但它随机决定,我的未更改的代码有一个问题,当它完美地工作之前,移动它。

以下是相关代码:

import discord
import asyncio
from discord.ext import commands
import os
from discord.utils import get
from dotenv import load_dotenv

load_dotenv()

currentcreator = 0

DISCORD_TOKEN = os.getenv("Token")

print("Logged on")

bot = commands.Bot(command_prefix="/")

@bot.command(name="embed")
@commands.cooldown(1, 30, commands.BucketType.user)
async def some_crazy_function_name(ctx, arg):
        argument = arg
        embed=discord.Embed(title=argument, url="https://www.test.com/", description="Description", color=0x3b05ff)
        embed.set_author(name="Author Name")
        embed.add_field(name="Field Name", value="Field Value", inline=False)
        await ctx.send(embed=embed)

@bot.command(name="start")
async def some_crazy_function_name(ctx):
        if not currentcreator == 0:
                await message.channel.send("Someone is already making a profile, please wait")
        currentcreater = ctx.author
        dir = r'C:\\Users\\FiercePC\Desktop\DiscordMMO\User-Profiles'
        MessageAuthor = str(ctx.author.id)
        ProfileDIR = os.path.join(dir,MessageAuthor)
        doesExist = os.path.exists(ProfileDIR)
        if doesExist == False:
                embed=discord.Embed(title="Creating Profile", url="", description=MessageAuthor+", your profile is being created", color=0x3b05ff)
                await ctx.send(embed=embed)
                os.makedirs(ProfileDIR,1)
                file = os.path.join(ProfileDIR,"Level.txt")
                open(file,"x")
                openfile = open(file,"w")
                openfile.write("0")
                openfile.close()
                file = os.path.join(ProfileDIR,"EXP.txt")
                open(file,"x")
                embed=discord.Embed(title="Creating Profile", url="", description=MessageAuthor+", your profile is being created", color=0x3b05ff)
                openfile = open(file,"w")
                openfile.write("0")
                openfile.close()
                file = os.path.join(ProfileDIR,"Strength.txt")
                open(file,"x")
                openfile = open(file,"w")
                openfile.write("1")
                openfile.close()
                embed=discord.Embed(title="Basic Profile Created", url='', description=MessageAuthor+', please use "/races" for a list of races, use "/raceinfo (Insert race here)" to view what bonus races give and finally use "/setrace (Insert race here)" to choose your race"', color=0x3b05ff)
                currentcreator = 0

@bot.command(name="races")
async def some_crazy_function_name(ctx):
        embed=discord.Embed(title="Race List", url="", description="Human,", color=0x3b05ff)
        await ctx.send(embed=embed)                

@bot.command(name="raceinfo")
async def some_crazy_function_name(ctx, arg):
        raceInfo = arg
        if raceInfo == "Human" or "human":
                embed=discord.Embed(title="Human Information", url="", description="Humans spread in this galaxy from their home planet of Coruscant, they are very good at adapting to all conditions thrown at them. Humans begin with a bonus 2 points in natural protection (reduced damage to natural elements like blizzards, sandstorms, freezing, etc) and they also begin with an extra point in speed (increased walking speed). Humans have a 7% initial force chance but it only increses by 0.34% when they level up (this bonus is only added up to level 21 when the final force roll takes place, if it fails you are permanently force un-sensetive)", color=0x3b05ff)        
                await ctx.send(embed=embed)

@bot.command(name="setrace")
async def some_crazy_function_name(ctx, arg):
        embed=discord.Embed(title="Human Information", url="", description="Human,", color=0x3b05ff)
        await ctx.send(embed=embed)

                
@bot.command(name="test")
async def foo(ctx, arg):
        await ctx.send(arg)
        await ctx.send(ctx.author)

bot.run('Token')


  

这是错误消息:

忽略命令开始中的异常:Traceback(最近一次调用最后一次):文件"C:\用户\FiercePC\AppData\本地\程序\Python\Python39\lib\site-包\不和谐\ext\命令\core.py",第85行,在包装的ret=wait coro(*args,**kwargs)文件"C:\用户\FiercePC\OneDrive\Desktop\Star_Wars_Combine线\脚本\Character_Creator.py",第30行,some_crazy_function_name如果不是当前创建者==0:无边界本地错误:分配前引用的本地变量'当前创建者'

上述异常是以下异常的直接原因:

Traceback(最近一次调用最后一次):文件"C:\用户\FiercePC\AppData\本地\程序\Python\Python39\lib\site-包\不和谐\ext\命令\bot.py",第902行,在调用等待ctx.command.invoke(ctx)文件"C:\用户\FiercePC\AppData\本地\程序\Python\Python39\lib\site-包\discord\ext\命令\core.py",第864行,在调用等待注入(*ctx.args,**ctx.kwargs)文件"C:\用户\FiercePC\AppData\本地\程序\Python\Python39\lib\site-包\discord\ext\命令\core.py",第94行,在包装中从exc引起命令输入错误(exc)discord.ext.commands.errors.命令输入错误:命令引起异常:Un 5/>",第94行:在赋值前引用的局部变量'当前创建者'

共有1个答案

阳兴文
2023-03-14

在函数中引用全局变量时,必须明确指定:

@bot.command(name="start")
async def some_crazy_function_name(ctx):
        global currentcreator
        if not currentcreator == 0:

如果未声明引用的是全局变量,Python将仅在函数块中查找该变量名,如果未找到赋值,则抛出UnboundLocalError

 类似资料:
  • Pillow 是对 PIL 的功能增加,想要在 Pillow 下运行 PIL 的代码,只需要: 把这个: import Image 修改成: from PIL import Image 注意,:py:mod:`_imaging` 模块已经被移除,现在可以这样导入:: from PIL.Image import core as _imaging 另外,图像插件导入机制已经改变。Pi

  • 所以, 我正在寻找一些帮助,使一个相当简单的页面更加移动/平板电脑友好,因为目前在某些移动设备上,它看起来非常丑陋:S 我试着正确定位东西,使用像素而不是百分比,因为我只在每个角落(顶部和底部栏)做东西,我试图结合一些缩放以及代码,使图像不显示在一定的屏幕宽度 有人能帮我纠正html/css,使其完全兼容吗?因为当我进行更改时,它从来都不起作用。 没有jsfiddle链接的原因是因为它不允许我把m

  • 问题内容: 有没有一种使用内置Java方法关闭计算机的方法? 问题答案: 创建自己的函数以通过命令行执行OS 命令? 为了举例。但是要知道,在其他地方以及为什么要使用它。

  • 问题内容: 我正在尝试从屏幕的底部到中间在图像视图上进行翻译动画。动画完成后,我希望图像视图停留在那里。我不要setFillAfter(true),因为我想更新imageview的实际位置。 目前,我通过具有2个图像视图(在动画开始时一个,在结束时一个)来做到这一点,并且我使用setVisibility来实现此目的。这是做事的正确方法吗?这是我使用的代码: 问题答案: 然后,必须为要设置动画的Vi

  • 问题内容: 我的PHP脚本向用户发送电子邮件,当电子邮件到达用户的邮箱时,主题行()的字符类似于添加到主题文本的末尾。这显然是编码问题。电子邮件内容本身很好,只是主题行中断了。 我已经搜索了一遍,但找不到 如何正确编码主题的方法 。 这是我的标题。请注意,我使用同和 。 问题答案: 更新 有关更实用和最新的答案,请查看Palec的答案。 Content-Type 中指定的字符编码仅描述消息正文的字

  • 本文向大家介绍python实现电脑自动关机,包括了python实现电脑自动关机的使用技巧和注意事项,需要的朋友参考一下 0、前言 在以前读书的时候,实验室里面要求每天都要关电脑,有时候出去玩得晚了,懒得回实验室关电脑,又没有同学帮忙。于是就想,能不能通过什么手段实现远程关闭电脑。在网上搜索一番后,决定用 python + email,实现远程关闭电脑。 1、准备 python: 3.5.2 ema