Issue
I was transitioning my old discord bot from 1.7.3 (before slash commands) to 2.3 and encountered an error when trying to send a message upon the use of a command. I have seen tutorials and they all use interaction.response.send_message() to send a message back in the channel where the command was taken.
Here is a snippet of my code:
import cmath
import datetime
import json
import os
import pickle
import youtube_dl
import discord
import wolframalpha
from ruamel.yaml import YAML
from discord.ext import commands
from discord import app_commands
from discord.utils import get
import requests
import pickle
import asyncio
import chatbot
print(discord.__version__)
client = wolframalpha.Client(OTHER_TOKEN)
yaml = YAML()
with open("./config.yml", "r", encoding = "utf-8") as file:
config = yaml.load(file)
bot = commands.Bot(command_prefix = config['Prefix'], intents=discord.Intents.all())
log_channel_id = config['Log Channel ID']
bot.embed_color = discord.Color.from_rgb(
config['Embed Settings']['Colors']['r'],
config['Embed Settings']['Colors']['g'],
config['Embed Settings']['Colors']['b']
)
bot.footer = config['Embed Settings']['Footer']['Text']
bot.footer_image = config['Embed Settings']['Footer']['Icon URL']
bot.prefix = config['Prefix']
bot.playing_status = config['Playing Status'].format(prefix = bot.prefix)
bot.TOKEN = os.getenv(config['Bot Token Variable Name'])
bot.members = []
bot.owner = [500033041545166864]
extensions = sorted([
'Cogs.general'
])
for extension in extensions:
bot.load_extension(extension)
bot.remove_command('help')
@bot.event
async def on_ready():
print(f"Logged in as {bot.user} and connected to discord! (ID: {bot.user.id})")
game = discord.Game(name = bot.playing_status)
await bot.change_presence(activity = game)
await bot.tree.sync()
embed = discord.Embed(
title = f"{bot.user.name} is online!",
color = bot.embed_color,
timestamp = datetime.datetime.now(datetime.timezone.utc)
)
embed.set_footer(
text = bot.footer,
icon_url = bot.footer_image
)
bot.log_channel = bot.get_channel(log_channel_id)
await bot.log_channel.send(embed = embed)
async def main():
async with bot:
await bot.start(TOKEN, reconnect=True)
@bot.tree.command(name="whois", description="Shows information about a member or yourself if left blank")
async def userinfo(interaction: discord.Interaction, member: discord.Member = None):
member = interaction.user if not member else member
print(member.color)
embed = discord.Embed(colour=member.color, timestamp=interaction.created_at)
embed.set_author(name=f"User Info - {member}")
print(member.avatar.url)
embed.set_thumbnail(url=member.avatar.url)
print(member.display_name)
embed.add_field(name="Name", value=str(member.display_name), inline = True)
print(member.top_role.mention)
embed.add_field(name="Top role", value=member.top_role.mention, inline = True)
print(member.bot)
embed.add_field(name="Bot?", value=member.bot, inline = True)
print("HI")
embed.set_author(
name=interaction.user.name,
icon_url=interaction.user.avatar.url
)
embed.set_footer(
text="Requested by " + interaction.user.name,
icon_url= bot.footer_image
)
await interaction.response.send_message(embed=embed)
await interaction.message.add_reaction('✅')
asyncio.run(main())
Here is the current output (there are a few warning but I believe they are not related to the issue at hand):
WARNING:tensorflow:From C:\Cloud\Programming\Python\Anaconda\envs\3.9_bot\lib\site-packages\tensorflow\python\compat\v2_compat.py:107: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.
Instructions for updating:
non-resource variables are not supported in the long term
curses is not supported on this machine (please install/reinstall curses for an optimal experience)
WARNING:tensorflow:From C:\Cloud\Programming\Python\Anaconda\envs\3.9_bot\lib\site-packages\tflearn\initializations.py:164: calling TruncatedNormal.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.
Instructions for updating:
Call initializer instance with the dtype argument instead of passing it to the constructor
C:\Cloud\Programming\Python\CodingProjects\Bot Code\main.py:48: RuntimeWarning: coroutine 'BotBase.load_extension' was never awaited
bot.load_extension(extension)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
2.3.2
Logged in as Wolf Extension#0723 and connected to discord! (ID: 741545843986923580)
For some reason, in my code, interact.response has no send_message() method. It just displays "Cannot find reference 'send_message' in '() -> InteractionResponse'" when I hover over the method in pycharm. I get no errors in my terminal though which may be due to all of the async activity that I don't fully understand. On the discord side, I just get "The application did not respond". Any help would be appreciated, thanks.
Solution
- Fix the warning by adding await to
await bot.load_extension(extension)
RuntimeWarning: coroutine 'BotBase.load_extension' was never awaited
- The PyCharm error is a known issue: the typing seems incomplete. At runtime, though, that method does exist.
You should do some more debugging to figure out where it's actually failing, and whether any connectivity works.
Answered By - Sébastien Vercammen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.