I need to get a list of roles of the user that executes a slash command. I can't seem to find a working solution, that would work with ctx and not with the user.Member or anything else.
My cog code:
from discord.commands import slash_command
from discord.ext import commands
class Cog(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.Cog.listener()
async def on_ready(self):
print("Cog connected.")
@slash_command(guild_ids=[12345], name='test')
async def test(self, ctx):
roles = """ here goes some code that fetches roles of the user who executed this command """
ctx.respond(f"Your roles are {roles}")
def setup(bot):
bot.add_cog(Cog(bot))
I am using Pycord 2.4.1, but I guess discord.py solutions would also work in my case. Thank you.
You can easily do this by using
for roles in member.roles
. This declares a variable called "roles" andmember.roles
checks what roles the member has.Here is the code:
Remember to declare a member role:
(Some of this code is based off of Rexy's code)
PS (just a small note), you can use slash commands by using:
This will only work if you use these two imports: