DEV Community

Cover image for PR Update
Antonio-Bennett
Antonio-Bennett

Posted on • Edited on

3 3

PR Update

Hey everyone! As usual the previous relevant post is here :)

What will we be covering?

Well....I got the code working!! So this blog will go into the code I needed to get this PR done. Honestly it was quite easy but I will get into why in the next blog of my final thoughts. Lets stick to the good stuff in this one :)

Code

So as I said in the previous blog I had to find a way to check if the server where the command request is sent has the DJ role. To do this I have this code

    let guild = msg.guild(&ctx.cache).await.unwrap();
    let mut roleid = RoleId::default();

    for (_, role) in guild.roles {
        if role.name == "DJ" {
            roleid = role.id;
        }
    }
Enter fullscreen mode Exit fullscreen mode

We need the Role id to check if the user has the DJ role. This is how we do that

    let user = &msg.author;
    user.has_role(&ctx.http, guild.id, roleid).await.unwrap()
Enter fullscreen mode Exit fullscreen mode

Now lets discuss the 2 strange variables in there. ctx and msg. ctxis the context of the event fired. This is used when the user tries to use a command to get guild information. msg is the message sent in the discord chat so you can find who sent it and their info.

What I did is then put this code into a utility function so it can be called to conditionally run a command if the user has the required role.

pub async fn author_is_dj(ctx: &Context, msg: &Message) -> bool {
    let guild = msg.guild(&ctx.cache).await.unwrap();
    let mut roleid = RoleId::default();

    for (_, role) in guild.roles {
        if role.name == "DJ" {
            roleid = role.id;
        }
    }

    let user = &msg.author;
    user.has_role(&ctx.http, guild.id, roleid).await.unwrap()
}
Enter fullscreen mode Exit fullscreen mode

It is then used like this in code

    if !author_is_dj(ctx, msg).await {
        //Inform user they do not have the required role
        //return
    } else {
        //Run command
    }
Enter fullscreen mode Exit fullscreen mode

What's next?

Well that was all for this blog! Hope you enjoyed it :) The next blog will be my final thoughts on this experience :)

Agent.ai Challenge image

Congrats to the Agent.ai Challenge Winners 🏆

The wait is over! We are excited to announce the winners of the Agent.ai Challenge.

From meal planners to fundraising automators to comprehensive stock analysts, our team of judges hung out with a lot of agents and had a lot to deliberate over. There were so many creative and innovative submissions, it is always so difficult to select our winners.

Read more →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay