Discord.py Function that responds for all commands - discord.py

I am looking for something like an event that is triggered every time any command is invoked. Something like:
#bot.event
async def on_command(ctx):
...
But I can't find anything for this. Does anyone know how to do this?

Ok lol, I searched the docs for on_command() and it turns out that there is an event called on_command(), so... I guess this is for anyone who had the same question as me :)

Related

AttributeError: 'User' object has no attribute 'guild_permissions'

enter image description here
Hi Hello I have a question, why did I actually get this error I want to know what he actually says and I would love to get a detailed explanation.
Firstly, please provide atleast some code for context. Secondly, don't post a screenshot of the error, just include it as a codeblock in your post. That being said, my best guess from the limited information is that you need to use ctx.message.author. In your error screenshot, it seems you are just using message.author.
The guild_permissions.administrator is not exists on discord.py and that's why you getting "User" object has no attribute 'guild_permissions' error and you do not need to post a screenshot here but it's okay.
Also do you have the has_permissions?
If you do not have then
from discord.ext import commands, tasks
from discord.ext.commands import is_owner, has_permissions, MissingPermissions, BadArgument
If you do, use
#client.command()
#has_permissions(administrator=True)
async def command(ctx):
#yourscript
That's just my opinion what i was thinking.
If you're using on_message then please provide a script what you did.

Can't intercept same service multiple times cypress.io

cy.intercept({
pathname: '/myService'
}).as('myServiceIntercept');
I would like to call:
cy.wait('#myServiceIntercept')
multiple times in my test but it only works the first time.
I also tried doing something like this:
cy.intercept({
pathname: '/myService'
}).as('myServiceIntercept2');
and then calling a wait:
cy.wait('#myServiceIntercept2')
but it seems like i can't override the first intercept and those wait basically won't work.
Can someone please help me getting on the right track?
Thanks!

How do I properly use asyncScheduler to know data was processed in rxjs?

I have some basic code like this:
this.events$
.pipe(
observeOn(asyncScheduler),
...
).subscribe(anotherObservable$);
This works fine in my app, but I am having a funny issue with a unit test. I put a couple of debug consoles like this:
this.events$
.pipe()
.subscribe(console.log.bind(null, 'sanity check inside without async'));
this.events$
.pipe(observeOn(asyncScheduler))
.subscribe(console.log.bind(null, 'sanity check inside with async'));
If I do from(events).subscribe(events$); in my test, the "without async" log fires.
If I do scheduled(events, asyncScheduler).subscribe(events$);, nothing fires.
I can't seem to fake the input to get my pipe on the async scheduler to fire. My test just needs that pipe to fire to see that something was called.
Realized right after posting:
scheduled(events, asyncScheduler).subscribe(events$);
await lastValueFrom(events$)

How can I make it so only a certain role can use a command with discord.py?

I'm attempting to make a command that only works with a certain role. I'm not 100% sure how to do this and I cannot find a solution anywhere else. My code stops short because I'm not very familiar with coding quite yet, but here it is:
#bot.command()
async def sign(ctx, member: discordMember):
if ctx.author.server_roles
From here I am completely lost and have no idea what to do.
The most efficient way to make it so a command can be used only with a certain role is the .has_role() decorator. You may put there a string with the role name (case sensitive) or the role ID (recommended), more info can be found in the documentation Here is an example:
#bot.command()
#commands.has_role("Administrator")
async def foo(ctx)
await ctx.send("bar")
If you would like to make it so the user can use this command only when he has any role then .has_any_role() would be the way to go, it takes strings or integers as well. You can find more info on it here. Here is a quick example on how that would work:
#bot.command()
#commands.has_any_role("Administrators", "Moderators", 492212595072434186)
async def foo(ctx):
await ctx.send("bar")
Happy Coding!
Usually when someone tries to execute such command with a .has_role decorator the discord.ext.commands.MissingRole is raised, so handling it would be something like this :
#bot.command()
#commands.has_any_role("Administrators", "Moderators", 492212595072434186)
async def foo(ctx):
try:
await ctx.send("bar")
except commands.MissingRole:
await ctx.send("lol")
Of course if you have a lot of commands, then I would recommend using a global error handler.

On *any* event discord.py

I want to run a certain block of code for every event. I figured that it would probably be easiest if there was something like on_any_event. However, I can't seem to find anything in the docs or on the web. Does anyone know if there is a way to do this, and if so, how?
Other Info:
discord.py-rewrite
Thanks in advance.
I see two possibilities:
Most of the events in discord.py, afaik, is "handlers" for sockets responses.
You can try to use on_socket_response(message) event. This should be enough for all websocket-based events.
If you need literally any event, you can try overwrite dispatch function in child class, and use this class as your's bot's class.
In example:
from discord.ext import commands
class MyBot(commands.Bot):
def dispatch(self, event_name, *args, **kwargs):
super().dispatch("event", event_name)
super().dispatch(event_name, *args, **kwargs)
bot = MyBot(command_prefix="!")
This will dispatch additional event on any event
#bot.event
async def on_event(event_name):
print(f"{event_name} is dispatched")

Resources