You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助修复Discord机器人/searchroblox命令的报错问题

修复Discord机器人/searchroblox命令报错问题

问题1:初始实现的RuntimeWarning与AttributeError

代码片段

main.py

from roblox import *
@tree.command(name = "searchroblox", description = "Sends roblox username", guild = discord.Object(id = 1080684956130496542))
async def searchroblox(ctx, *, username: str):
  robloxuser = robloxUser(username)
  embed = discord.Embed(color=0xa3fff7, title=f"Info for {username}")
  embed.add_field(name="Username",value="`" + username + "`")
  embed.add_field(name="Display Name", value="`" + robloxuser.getdn() + "`")
  embed.add_field(name="User ID", value="`" + str(robloxuser.getid()) + "`")
  embed.add_field(name="Description", value= "```" + (robloxuser.getdesc() or "No description") + "```")
  await ctx.response.send_message(embed=embed)
  if isinstance(ctx.channel, discord.DMChannel):
   return

roblox.py

from ro_py import Client
robloxclient = Client()

class robloxUser:
    def __init__(self, rblox):
        rbloxuser = robloxclient.get_user_by_username(rblox)
    def username(self, rbloxuser):
        self.__user = rbloxuser.name
        
    def display_name(self, rbloxuser):
        self.__dn = rbloxuser.display_name
        
    def userid(self, rbloxuser):
        self.__id = rbloxuser.id
        
    def description(self, rbloxuser):
        self.__description = rbloxuser.description or "No description"

    def getruser(self):
        return self.__user
    def getdn(self):
        return self.__dn
    def getid(self):
        return self.__id
    def getdesc(self):
        return self.__description

错误日志

main.py:145: RuntimeWarning: coroutine 'Client.get_user_by_username' was never awaited
  robloxuser = robloxUser(username)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
2023-04-03 20:30:43 ERROR    discord.app_commands.tree Ignoring exception in command 'searchroblox'
Traceback (most recent call last):
  File "/home/runner/Pixel/venv/lib/python3.10/site-packages/discord/app_commands/commands.py", line 842, in _do_call
    return await self._callback(interaction, **params)  # type: ignore
  File "main.py", line 148, in searchroblox
    embed.add_field(name="Display Name", value="`" + robloxuser.getdn() + "`")
  File "/home/runner/Pixel/roblox.py", line 22, in getdn
    return self.__dn
AttributeError: 'robloxUser' object has no attribute '_robloxUser__dn'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/home/runner/Pixel/venv/lib/python3.10/site-packages/discord/app_commands/tree.py", line 1248, in _call
    await command._invoke_with_namespace(interaction, namespace)
  File "/home/runner/Pixel/venv/lib/python3.10/site-packages/discord/app_commands/commands.py", line 867, in _invoke_with_namespace
    return await self._do_call(interaction, transformed_values)
  File "/home/runner/Pixel/venv/lib/python3.10/site-packages/discord/app_commands/commands.py", line 860, in _do_call
    raise CommandInvokeError(self, e) from e
discord.app_commands.errors.CommandInvokeError: Command 'searchroblox' raised an exception: AttributeError: 'robloxUser' object has no attribute '_robloxUser__dn'

修复方案

  1. 解决RuntimeWarning:get_user_by_username是协程函数,必须用await调用,同时给robloxUser添加异步类方法来初始化实例。
  2. 解决AttributeError:原代码中username、display_name等方法从未被调用,导致私有属性未初始化,需在获取用户对象后直接赋值。

修改后的roblox.py:

from ro_py import Client
from ro_py.utilities.errors import UserDoesNotExistError

robloxclient = Client()

class robloxUser:
    def __init__(self, rbloxuser):
        self.__user = rbloxuser.name
        self.__dn = rbloxuser.display_name
        self.__id = rbloxuser.id
        self.__description = rbloxuser.description or "No description"

    @classmethod
    async def from_username(cls, username):
        try:
            rbloxuser = await robloxclient.get_user_by_username(username)
            return cls(rbloxuser)
        except UserDoesNotExistError:
            return None

    def getruser(self):
        return self.__user
    def getdn(self):
        return self.__dn
    def getid(self):
        return self.__id
    def getdesc(self):
        return self.__description

修改后的main.py调用部分:

from roblox import robloxUser
@tree.command(name = "searchroblox", description = "Sends roblox username", guild = discord.Object(id = 1080684956130496542))
async def searchroblox(ctx, *, username: str):
  robloxuser = await robloxUser.from_username(username)
  if not robloxuser:
      await ctx.response.send_message(f"用户`{username}`不存在")
      return
  embed = discord.Embed(color=0xa3fff7, title=f"Info for {username}")
  embed.add_field(name="Username",value="`" + username + "`")
  embed.add_field(name="Display Name", value="`" + robloxuser.getdn() + "`")
  embed.add_field(name="User ID", value="`" + str(robloxuser.getid()) + "`")
  embed.add_field(name="Description", value= "```" + robloxuser.getdesc() + "```")
  await ctx.response.send_message(embed=embed)

问题2:简化代码后的UserDoesNotExistError

代码片段

import ro_py
from ro_py import Client
@tree.command(name = "searchroblox", description = "Sends roblox username", guild = discord.Object(id = 1080684956130496542))
async def searchroblox(ctx, *, username: str):
  x = Client()

  robloxuser = await x.get_user_by_username(username)
  embed = discord.Embed(color=0xa3fff7, title=f"Info for {username}")
  embed.add_field(name="Username",value="`" + username + "`")
  embed.add_field(name="Display Name", value="`" + robloxuser.display_name + "`")
  embed.add_field(name="User ID", value="`" + str(robloxuser.id) + "`")
  embed.add_field(name="Description", value= "```" + (robloxuser.description or "No description") + "```")
  await ctx.response.send_message(embed=embed)
  if isinstance(ctx.channel, discord.DMChannel):
   return

错误日志

Traceback (most recent call last):
  File "/home/runner/Pixel/venv/lib/python3.10/site-packages/discord/app_commands/commands.py", line 842, in _do_call
    return await self._callback(interaction, **params)  # type: ignore
  File "main.py", line 147, in searchroblox
    robloxuser = await x.get_user_by_username(username)
  File "/home/runner/Pixel/venv/lib/python3.10/site-packages/ro_py/client.py", line 131, in get_user_by_username
    raise UserDoesNotExistError
ro_py.utilities.errors.UserDoesNotExistError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/runner/Pixel/venv/lib/python3.10/site-packages/discord/app_commands/tree.py", line 1248, in _call
    await command._invoke_with_namespace(interaction, namespace)
  File "/home/runner/Pixel/venv/lib/python3.10/site-packages/discord/app_commands/commands.py", line 867, in _invoke_with_namespace
    return await self._do_call(interaction, transformed_values)
  File "/home/runner/Pixel/venv/lib/python3.10/site-packages/discord/app_commands/commands.py", line 860, in _do_call
    raise CommandInvokeError(self, e) from e
discord.app_commands.errors.CommandInvokeError

修复方案

该错误是因为输入的Roblox用户名不存在,需要捕获UserDoesNotExistError异常返回友好提示,同时全局复用Client实例避免重复创建。

修改后的代码:

import ro_py
from ro_py import Client
from ro_py.utilities.errors import UserDoesNotExistError

# 全局复用Client实例,避免每次创建
roblox_client = Client()

@tree.command(name = "searchroblox", description = "Sends roblox username", guild = discord.Object(id = 1080684956130496542))
async def searchroblox(ctx, *, username: str):
  try:
    robloxuser = await roblox_client.get_user_by_username(username)
  except UserDoesNotExistError:
      await ctx.response.send_message(f"未找到Roblox用户`{username}`")
      return
  
  embed = discord.Embed(color=0xa3fff7, title=f"Info for {username}")
  embed.add_field(name="Username",value=f"`{username}`")
  embed.add_field(name="Display Name", value=f"`{robloxuser.display_name}`")
  embed.add_field(name="User ID", value=f"`{robloxuser.id}`")
  embed.add_field(name="Description", value=f"```{(robloxuser.description or 'No description')}```")
  await ctx.response.send_message(embed=embed)

内容的提问来源于stack exchange,提问作者Dark

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 23:57:11