命令执行时 discord.py "get is not defined"错误如何修复?



最近我一直在用python编写我的bot,因为我是bot的初学者等等,我想做一个meme命令,因为meme是bot的目标,当我执行memr命令时,它说一切都很好,但是当有人运行meme命令,它只是说"discord.ext.commands.errors.CommandInvokeError:命令引发了一个异常:NameError:名称'get'未定义。我不明白为什么会这样。我相信StackOverflow的力量!下面是我的代码:

import discord.ext
import asyncio
from discord.ext import commands
import random
import os
from datetime import datetime
import json
PREFIX = ("f!", "F!")
bot = commands.Bot(command_prefix=PREFIX, description='the bot is currently being redesigned but you can use the commands below like f!say :)', case_insensitive=True)
@bot.event
async def on_ready():
activity = discord.Game(name="f!help")
await bot.change_presence(status=discord.Status.online, activity=activity)
print("bot is ready")
@bot.event
async def on_guild_join(guild):
for channel in guild.text_channels:
if channel.permissions_for(guild.me).send_messages:
await channel.send('thanks for inviting me to your server! try saying "f!help" to get my commands!')
break
@bot.command(description="shows you my ping", brief="shows you my ping")
async def ping(ctx):
await ctx.send(f'my ping is {bot.latency}ms!')
@bot.command(description="makes me say whatever you want", brief="makes me say whatever you want me to say")
async def say(ctx, *, message):
await ctx.message.delete()
await ctx.send(f'{message}')
@commands.Cog.listener()
async def on_member_join(self, member):
with open('users.json', 'r')as f:
users = json.load(f)
await self.update_data(users, member)
with open('users.json', 'w')as f:
json.dump(users, f)
@commands.Cog.listener()
async def on_message(self, message):
if not message.author.bot:
with open('users.json','r')as f:
users = json.load(f)
await self.update_data(users, message.author)
if(users[str(message.author.id)]['LastMessage'] < await self.to_integer(datetime.now())):
await self.add_experience(users, message.author)
await self.level_up(users, message.author, message.channel)

with open('users.json', 'w')as f:
json.dump(users, f)
async def update_data(self, users, user):
if not str(user.id) in users:
users[str(user.id)] = {}
users[str(user.id)]['experience'] = 0
users[str(user.id)]['level'] = 0
users[str(user.id)]['LastMessage'] = await self.to_integer(datetime.now())
async def add_experience(self, users, user):
users[str(user.id)]['experience'] += random.randint(15,25)
users[str(user.id)]['LastMessage'] = await self.to_integer(datetime.now())

async def level_up(self, users, user,message):
experience = users[str(user.id)]['experience']
lvl = users[str(user.id)]['level']
lvl_end = 5 * (lvl ** 2) + (50 * lvl) + 100
print(user)
print(f"Level:{lvl}")
print(f"experience:{experience}")
print(f"lvl_end: {lvl_end} ")

if lvl_end <= experience:
channel=self.client.get_channel(810855960133894154)
await channel.send('{} has leveld up to level {}'.format(user.mention, lvl+1))
users[str(user.id)]['level'] = lvl+1
users[str(user.id)]['experience'] -= lvl_end

async def to_integer(self, dt_time):
answer = 100000000 * dt_time.year + 1000000 * dt_time.month + 10000 * dt_time.day + 100 * dt_time.hour + dt_time.minute
return int(answer)
@commands.command()
async def rank(self,ctx, user: discord.Member = None):
with open('users.json','r')as f:
users = json.load(f)
if user is None:
if not str(ctx.author.id) in users:
users[str(ctx.author.id)] = {}
users[str(ctx.author.id)]['experience'] = 0
users[str(ctx.author.id)]['level'] = 0
users[str(ctx.author.id)]['LastMessage'] = await self.to_integer(datetime.now())
user=ctx.author
lvl = int(users[str(ctx.author.id)]['level'])
exp = int(5 * (lvl ** 2) + (50 * lvl) + 100)
embed = discord.Embed(Title=f"**{user}'s Rang**",Description=f"Experience: {lvl}/{5 * (lvl ** 2) + (50 * lvl) + 100}", color=0x0091ff)
embed.set_thumbnail(url=f"{user.avatar_url}")
embed.add_field(name=f"**{user}'s Rang**", value="💪  ", inline=False)
embed.add_field(name="Level", value=f"**{users[str(user.id)]['level']}**", inline=True)
embed.add_field(name="Experience", value=f"**{str(int(users[str(user.id)]['experience']))} / {exp}**",inline=True)
embed.set_footer(text="Type more to level up!nSpam is useless")
await ctx.send(embed=embed)

else:
if not str(user.id) in users:
users[str(user.id)] = {}
users[str(user.id)]['experience'] = 0
users[str(user.id)]['level'] = 0
users[str(user.id)]['LastMessage'] = await self.to_integer(datetime.now())
lvl = int(users[str(user.id)]['level'])
exp=int(5 * (lvl ** 2) + (50 * lvl) + 100)
embed=discord.Embed(Title=f"**{user}'s Rang**",Description=f"Experience: {lvl}/{5 * (lvl ** 2) + (50 * lvl) + 100}",color=0x0091ff)
embed.set_thumbnail(url=f"{user.avatar_url}")
embed.add_field(name=f"**{user}'s Rang**", value="💪  ", inline=False)
embed.add_field(name="Level",value=f"**{users[str(user.id)]['level']}**",inline=True)
embed.add_field(name="Experience", value=f"**{str(int(users[str(user.id)]['experience']))} / {exp}**", inline=True)
embed.set_footer(text="Type more to level up!nSpam is useless")
await ctx.send(embed=embed)
with open('users.json', 'w')as f:
json.dump(users, f)
@commands.command()
@commands.has_permissions(administrator=True)
async def add_exp(self, ctx, user: discord.Member,nummer:int):
with open('users.json', 'r')as f:
users = json.load(f)
if not ctx.author.bot:
users[str(user.id)]['experience']+=int(nummer)
await ctx.send("exp added")
with open('users.json', 'w')as f:
json.dump(users, f)
@commands.command()
async def add_lvl(self, ctx, user: discord.Member,nummer:int):
with open('users.json', 'r')as f:
users = json.load(f)
if not ctx.author.bot:
users[str(user.id)]['level'] += int(nummer)
await ctx.send("level added")
with open('users.json', 'w')as f:
json.dump(users, f)
@commands.command()
async def add_database(self,ctx, user: discord.Member):
with open('users.json', 'r')as f:
users = json.load(f)
if not str(user.id) in users:
users[str(user.id)] = {}
users[str(user.id)]['experience'] = 0
users[str(user.id)]['level'] = 0
users[str(user.id)]['LastMessage'] = await self.to_integer(datetime.now())
await ctx.send("added to database!")
else:
await ctx.send("already in database!")
with open('users.json', 'w')as f:
json.dump(users, f)

def setup(client):
client.add_cog(levels(client))
@bot.command(pass_context=True, description='some fresh memes for you!')
async def meme(ctx):
content = get("https://meme-api.herokuapp.com/gimme/dankmemes").text
data = json.loads(content,)
meme = discord.Embed(title=data['title'], url=data['postLink'], color = colors.purple, timestamp=ctx.message.created_at)
meme.set_image(url=f"{data['url']}")
meme.set_footer(text=f'Used by {ctx.author.name}', icon_url=ctx.author.avatar_url)
await ctx.reply(embed=meme)
bot.run('bot token here')```

在函数meme中,您正在调用函数get以从某些URL获取数据。但是,函数get在任何地方都没有定义,导致错误NameError: name 'get' is not defined。您可能需要从requests模块导入它。

将以下行添加到文件顶部:

from requests import get

这个链接有更多关于requests.get方法的信息。

相关内容

  • 没有找到相关文章

最新更新