0

I was trying to make a Discord bot command to retrieve a tenor gif, so I asked it to send me the json text in discord.

When I run the command (which is !cat), the error {'code': 16, 'error': 'API key invalid.'} is sent by my discord bot. I had just registered a Tenor API key. My code:

import random
import discord
import time
import json
import requests
# import tenor
from os import *
from discord.ext import commands
from discord.ext.commands import has_permissions, MissingPermissions

class MyClient(discord.Client):
    async def on_ready(self, message):
        print(f'[Client] Starting discord bot {self.user}')
        print(f'Logged on as {self.user}!')
        
        

    def setup(bot):
        bot.add_cog(Cog(bot))
    async def on_message(self, message):
        print(f'Message from {message.author}: {message.content}')
        


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




@client.command()
async def commands(ctx):
    embed = discord.Embed(title="Commands")
    embed.add_field(name="Admin and Non Admin commands", value="Commands that can be used by admins and non-admins", inline=False)
    embed.add_field(name="!avatar", value="Get persons avatar!", inline=False)
    embed.add_field(name="!info", value="Shows info about the bot", inline=False)
    embed.add_field(name="!users", value="Shows how many members the server has", inline=False)
    embed.add_field(name="Admin only commands", value="Commands that can be used by admins only.", inline=False)
    embed.add_field(name="!ban", value="Ban a user", inline=False)
    embed.add_field(name="!unban", value="Unbans a user", inline=False)
    embed.add_field(name="!kick", value="Kicks a user", inline=False)
    await ctx.send(content="More commands coming soon", embed=embed)

@client.command()
async def info(ctx):
    embed = discord.Embed(title="Bot statistics")
    embed.add_field(name="Bot username", value="<@1050289486124306452>")
    embed.add_field(name="Bot Author", value="RobinAnd#4304")
    await ctx.send(content="Current stats for Cat-bot", embed=embed)

@client.command()
async def users(ctx):
    await ctx.send(f"""This server has {ctx.guild.member_count} members""")

@client.command()

async def sendonline(ctx):
    await ctx.send("This bot uses the !commands command as the help command")

@client.command()
async def echo(ctx, arg):
    await ctx.send(arg)
print("Bot is online and all modules are loaded!")

@client.command()
@has_permissions(ban_members = True)
async def ban (ctx, member:discord.User=None, reason =None):
    if member == None or member == ctx.message.author:
        await ctx.channel.send("You cannot ban yourself")
        return
    if reason == None:
        reason = "For being a jerk!"
    message = f"You have been banned from {ctx.guild.name} for {reason}"
    await member.send(message)
    await ctx.guild.ban(member, reason=reason)
    await ctx.channel.send(f"{member} is banned!")

@client.command()
@has_permissions(administrator = True)
async def unban(ctx, *, member):
    banned_users = await ctx.guild.bans()
    member_name, member_discriminator = member.split("#")

    for ban_entry in banned_users:
        user = ban_entry.user

        if (user.name, user.discriminator) == (member_name, member_discriminator):
            await ctx.guild.unban(user)
            await ctx.send(f'Unbanned {user.mention}')
            return
@client.command()
@has_permissions(kick_members = True)
async def kick(ctx, user: discord.Member, *, reason = None):
  if not reason:
    await user.kick()
    await ctx.send(f"**{user}** has been kicked for **no reason**.")
  else:
    await user.kick(reason=reason)
    await ctx.send(f"**{user}** has been kicked for **{reason}**.")

@client.command()
async def avatar(ctx, *, member: discord.Member = None):
    if not member:
        member = ctx.message.author
    userAvatar = member.avatar_url
    await ctx.send(userAvatar)

@client.command()
async def cat(ctx):
    apikey = "Tenor API key (hdifhaifjhdf-faf)"  # test value
    lmt = 20

    search_term = "cute cat"

    # get the top 8 GIFs for the search term using default locale of EN_US
    r = requests.get(
        "https://g.tenor.com/v1/search?q=%s&key=%s&limit=%s" % (search_term, apikey, lmt))

   
    # load the GIFs using the urls for the smaller GIF sizes
    top_8gifs = json.loads(r.content)
    await ctx.send(top_8gifs)
client.run('token')

See the attached image for the output. I am running Python 3.10.6

I was expecting that I would show my tenor key, but the Invalid API key error came up. I even tried regenerating a new one. That still didn't work.

Phil
  • 157,677
  • 23
  • 242
  • 245

0 Answers0