我正在将旧的不和谐机器人从 1.7.3(斜杠命令之前)过渡到 2.3,并在尝试使用命令发送消息时遇到错误。我看过教程,它们都使用interaction.response.send_message() 在执行命令的通道中发送消息。
这是我的代码片段:
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())
这是当前的输出(有一些警告,但我相信它们与当前的问题无关):
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)
由于某种原因,在我的代码中,interact.response 没有 send_message() 方法。当我将鼠标悬停在 pycharm 中的方法上时,它只是显示“在 '() -> InteractionResponse' 中找不到引用 'send_message'”。我的终端没有收到任何错误,但这可能是由于我不完全理解的所有异步活动造成的。在不和谐方面,我只是得到“应用程序没有响应”。任何帮助将不胜感激,谢谢。
await bot.load_extension(extension)
运行时警告:从未等待过协程“BotBase.load_extension”
您应该进行更多调试,以找出实际失败的位置以及连接是否有效。