英文:
I am working on a roblox search slash command but it isn't exactly working correctly, I would like some support of revamping these
问题
I want to make a /searchroblox command. Below is what I have
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
After running the code it would show.
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'
What would I have to do to fix this. I commented all this to avoid problems with the rest of my code by the way.
robloxUser keeps saying it doesn't connect with ro_py file set after I do use it.
Edits: I removed roblox.py:
main.py:
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
This is my new code, below shows what I got, I am commenting it again just in case it crashes my other code.
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: Command 'searchroblox' raised an exception: UserDoesNotExistError:
英文:
I want to make a /searchroblox command. Below is what I have
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
After running the code it would show.
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'
What would I have to do to fix this. I commented all this to avoid problems with the rest of my code by the way.
robloxUser keeps saying it doesn't connect with ro_py file set after I do use it.
Edits: I removed roblox.py:
main.py:
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
This is my new code, below shows what I got, I am commenting it again just in case it crashs my other code.
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
: Command 'searchroblox' raised an exception: UserDoesNotExistError:
答案1
得分: 0
错误
RuntimeWarning: 启用tracemalloc以获取对象分配跟踪
这个警告意味着代码未等待。所使用的函数是异步的,这意味着你必须使用await
关键字。await
关键字只能在异步函数中使用。
你不需要创建一个完整的roblox.py文件
库已经为你完成了所有的工作。而是在你已经的异步函数内执行以下操作:
robloxuser = await client.get_user_by_username(username)
然后在嵌入中,你可以使用robloxuser
变量的属性。
embed.add_field(name="用户ID", value="`" + str(robloxuser.id) + "`")
embed.add_field(name="描述", value="```" + (robloxuser.description or "没有描述") + "```")
在这里可以看到一个不错的示例。
你的更新
你的错误:
: 命令'searchroblox'引发了一个异常: UserDoesNotExistError:
这只是意味着你输入的用户不存在。使用try except语句来返回给运行命令的用户。
英文:
The error
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
This warning means that the code was not awaited. The function used is asynchronous meaning that you have to use the await
keyword. The await keyword can only be used in asynchronous functions.
You don't need to create an entire roblox.py file
The library does all the work for you. Instead, do the following within your already async function:
robloxuser = await client.get_user_by_username(username)
Then in the embed, you can use the properties of the robloxuser
variable.
embed.add_field(name="User ID", value="`" + str(robloxuser.id) + "`")
embed.add_field(name="Description", value= "```" + (robloxuser.description or "No description") + "```")
See a nice example here.
Your update
Your error:
: Command 'searchroblox' raised an exception: UserDoesNotExistError:
This just means that the user you inputted does not exist. Use a try except statement to return that to the user who ran the command.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论