discord-py-ui / discord-ui Goto Github PK
View Code? Open in Web Editor NEWA discord.py extension for sending, receiving and handling ui interactions in discord
Home Page: https://discord-ui.rtfd.io/
License: MIT License
A discord.py extension for sending, receiving and handling ui interactions in discord
Home Page: https://discord-ui.rtfd.io/
License: MIT License
When using discord-ui
, any attempt to perform an await ctx.author.send(...)
results in the following error:
File "/opt/homebrew/lib/python3.9/site-packages/discord_ui/override.py", line 50, in send
r = await self._state.http.request(route, json=payload)
File "/opt/homebrew/lib/python3.9/site-packages/discord/http.py", line 250, in request
raise NotFound(r, data)
discord.errors.NotFound: 404 Not Found (error code: 10003): Unknown Channel
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/homebrew/lib/python3.9/site-packages/discord/ext/commands/bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "/opt/homebrew/lib/python3.9/site-packages/discord/ext/commands/core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/opt/homebrew/lib/python3.9/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NotFound: 404 Not Found (error code: 10003): Unknown Channel
Currently, I'm working around it by manually creating a DM channel, but it's an ugly kludge.
Performing a simple await ctx.reply()
does not actually reply. The message is sent without tying it to the appropriate message/context.
The same is true for await ctx.message.reply()
.
When creating the extension as part of a custom bot class, you get an AttributeError.
I'm using Python 3.9.6, discord.py version 1.7.3 and discord-ui version 3.1.0.
Traceback (most recent call last):
File "main.py", line 10, in <module>
bot = CustomBot(command_prefix="!")
File "main.py", line 8, in __init__
self.ui = UI(self)
File "discord_ui/client.py", line 1038, in __init__
self.components = Components(client, auto_defer=auto_defer)
File "discord_ui/client.py", line 810, in __init__
elif discord.__version.startswith("1"):
AttributeError: module 'discord' has no attribute '_Components__version'
(I removed unnecessary parts of the paths)
Here's the code:
from discord.ext import commands
from discord_ui import *
class CustomBot(commands.Bot):
def __init__(self, **options):
super().__init__(**options)
self.ui = UI(self)
bot = CustomBot(command_prefix="!")
bot.run("token here")
The documentation suggests that it has to be a commands.Bot
, but I don't see how this would break it.
Additionally, this only started happening once I updated to 3.1.0.
(Please point out any mistakes, if I get too confused.)
I created a guild-only slash command, as follows:
bs = Slash(bot)
...
@bs.command(name= "test",
description= "Foo bar", guild_ids= [880686520678371369],
default_permission= False,
guild_permissions= {880686520678371369: ui.SlashPermission(allowed= {ui.SlashPermission.USER: [687474789342117900]})}
)
async def tester(ctx):
await ctx.respond("Test")
# 687474789342117900 is my user ID
I invoked this slash command. Immediately after that, the command gets removed from the list of other commands. All other commands continued to work, so I believe it is not a fatal bug. I tried looking into the module's code but I cannot find anything - I think it might be the bot wrongly deleting the command?
The official discord.py library supports loading extension commands from other files, so I think it is a good idea for discord_ui to have it as well. Otherwise, large bots can easily get to tens of thousands of lines in a single file.
To put this conveniently, here is the bot.load_extension()
method definition:
# In the Bot class
def load_extension(
self,
name: str,
*,
package: Optional[str] = None,
extras: Optional[Dict[str, Any]] = None
) -> None:
name = self._resolve_name(name, package)
if name in self.__extensions:
raise errors.ExtensionAlreadyLoaded(name)
spec = importlib.util.find_spec(name)
if spec is None:
raise errors.ExtensionNotFound(name)
self._load_from_module_spec(spec, name, extras=extras)
When overridden, ctx.send
does raises an exception if you pass in a file
or files
kwarg, while the regular discord.py ctx.send
does not.
After peeking at the source, it seems that the jsonifyMessage
function wants the discord.Attachment
class - which is the class for an attachment on an already existing message. It also tries to put it in the attachments
field of the payload
object, which the Discord API does not support (according to https://discord.com/developers/docs/resources/channel#create-message-jsonform-params)
Responding to a select menu interaction using ninja mode gives a 404 Not Found, with error code 10062 (Unknown interaction). I tested with sending a message, and it works. (I'm receiving the event through a @commands.Cog.listener("on_menu_select")
decorated function, since I couldn't find a way to use the other ways with cogs; actually, better support for cogs would be great). Sorry for not providing an example, I'm in a hurry right now.
Per the dpy docs, await msg.edit(embed=None)
is supposed to remove an embed from a message. However, I receive the following error:
Traceback (most recent call last):
File "/opt/homebrew/lib/python3.9/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "/opt/homebrew/lib/python3.9/site-packages/discord_ui/client.py", line 257, in _on_response
await x.invoke(context, **options)
File "/opt/homebrew/lib/python3.9/site-packages/discord_ui/cogs.py", line 98, in invoke
raise ex
File "/opt/homebrew/lib/python3.9/site-packages/discord_ui/cogs.py", line 93, in invoke
await self.callback(self.cog, ctx, *args, **kwargs)
File "/Users/tiltowait/Developer/inconnu/interface/characters.py", line 104, in delete_character
await inconnu.delete.prompt(ctx, character)
File "/Users/tiltowait/Developer/inconnu/inconnu/delete.py", line 28, in prompt
await msg.edit(embed=None)
File "/opt/homebrew/lib/python3.9/site-packages/discord_ui/receive.py", line 668, in edit
self._update(await self._state.http.request(r, json=jsonifyMessage(*args, **fields)))
File "/opt/homebrew/lib/python3.9/site-packages/discord_ui/http.py", line 70, in jsonifyMessage
raise WrongType("embeds", embeds, 'list[discord.Embed]')
discord_ui.errors.WrongType: 'embeds' must be of type list[discord.Embed], not <class 'list'>
Out of curiosity, I tried embeds=None
but got a different error:
Traceback (most recent call last):
File "/opt/homebrew/lib/python3.9/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "/opt/homebrew/lib/python3.9/site-packages/discord_ui/client.py", line 257, in _on_response
await x.invoke(context, **options)
File "/opt/homebrew/lib/python3.9/site-packages/discord_ui/cogs.py", line 98, in invoke
raise ex
File "/opt/homebrew/lib/python3.9/site-packages/discord_ui/cogs.py", line 93, in invoke
await self.callback(self.cog, ctx, *args, **kwargs)
File "/Users/tiltowait/Developer/inconnu/interface/characters.py", line 104, in delete_character
await inconnu.delete.prompt(ctx, character)
File "/Users/tiltowait/Developer/inconnu/inconnu/delete.py", line 28, in prompt
await msg.edit(embeds=None)
File "/opt/homebrew/lib/python3.9/site-packages/discord_ui/receive.py", line 668, in edit
self._update(await self._state.http.request(r, json=jsonifyMessage(*args, **fields)))
File "/opt/homebrew/lib/python3.9/site-packages/discord_ui/http.py", line 69, in jsonifyMessage
if not all(isinstance(x, discord.Embed) for x in embeds):
TypeError: 'NoneType' object is not iterable
According to the docs, discord_ui.Message.wait_for("select", ...)
returns a SelectedMenu
item; however, in my testing, it returns a ComponentContext
object. Importantly, the latter lacks a selected_values
item; in order to access the selection, I have to use component.data["values"]
.
This discrepancy also affects this line in the example code.
Hey, I tried today to rewrite my Discord bot to Nextcord but somehow I can't because discord-ui requires discord.py and the lib of Discord-UI doesn't support Nextcord, is there any way to fix this?
Before the question comes up, I am using discord-ui version 5.1.5
Error message: discord.errors.HTTPException: 400 Bad Request (error code: 50006): Cannot send an empty message
Posting here for the sake of organization.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.