Sean Gilbert
Sean Gilbert

Reputation: 55

I keep getting an error in my discord bot, and cannot figure out how to fix it

I have been trying to help a friend fix his discord bot, but I'm stumped on the solution.

from discord.ext import commands
import bs4
import requests
import time
import lxml



adminkey = "CheeseIsTheBest"
keys = open('keys.txt', 'w')

TOKEN = "token goes here"

client = commands.Bot(command_prefix = "!")

client.remove_command('help')

@client.event
async def on_ready():
    print("Locked And Loaded Mr. DarkKnight")

@client.command(name='ping')
async def ping(ctx):
    await ctx.send('pong')

@client.command()
async def addkey(ctx, key):
    if str(ctx.author) == "ironkey#6969" or str(ctx.author) == "ZERO#2020":
        with open('keys.txt', 'r+') as file :
            file.write(str(key + "\n"))
            file.close()
        await ctx.send('key added')
    else:
        await ctx.send('your not authorized to preform this action ' + str(ctx.author))


@client.command()
async def redeemkey(ctx, key):
    with open('keys.txt', 'r+') as file:
        for line in file:
            if str(key.strip()) == str(line.strip()):
                with open('keys.txt', 'w') as file:
                    keyfile = keyfile.replace(str(key), key + " X")
                    file.write(keyfile)
                    file.close()
                    await ctx.send('key redeemed!')
            else:
                await ctx.send('This key has already been used')

@client.command()
async def unbind(ctx, *, member):
    role = discord.utils.get(ctx.guild.roles, name='authenticated')
    await client.remove_roles(member, role)


@client.command(aliases=['inv'])
async def invite(ctx):
    await ctx.send('https://discordapp.com/api/oauth2/authorize?client_id=645664026160005129&permissions=8&scope=bot')

@client.command()
async def help(ctx):
    embed = discord.Embed(color=discord.Color.dark_blue())
    embed.set_author(name="Cheese Auth Help Menu", icon_url="")
    embed.add_field(name=".ping", value="a simple fun command! usage : .ping")
    embed.add_field(name=".invite", value="also used with the command inv, this command will get you an invite link so you can add this bot to your server! usage: .invite")
    await ctx.send(embed=embed)

client.run(TOKEN)

Every time I try to redeem the key I added, I get this error:

Ignoring exception in command redeemkey:

Traceback (most recent call last):

File "C:\Users\seang\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 79, 
in wrapped ret = await coro(*args, **kwargs) 
File "C:\Users\seang\Downloads\Bot_for_DarkKnight1-_Copy.py", line 41, 
in redeemkey keyfile = keyfile.replace(str(key + X)) 
UnboundLocalError: local variable 'keyfile' referenced before assignment.

Upvotes: 0

Views: 75

Answers (2)

lh1395
lh1395

Reputation: 170

The error comes from this line:

keyfile = keyfile.replace(str(key), key + " X")

You're trying to make a new value called 'keyfile', but it relies on information which comes from 'keyfile', which doesn't exist yet.

Upvotes: 0

backtrack
backtrack

Reputation: 8144

From your code i noticed that keyfile.replace is the problem. Variable keyfile is not assigned or declared (defined) yet.You are reading a file line by line in variable line so I think your code should be

keyfile = line.replace(str(key), key + " X")

Upvotes: 1

Related Questions