0

I am trying to get my discord bot to assign a role to a user when they input the command '!stream' I am very new to programming in c# and discord.net especially. Right now I have the code that should be assigning the role inside of a message received method. This probably isn't the best way to do it but I'm not super sure a better way right now. Anyways, here is the code:

IGuildUser user = (IGuildUser)arg.Author;
IRole role = ((IGuildChannel)arg.Channel).Guild.GetRole(theRoleID);
if (arg.Content == "!stream") {
    await (user as IGuildUser).AddRoleAsync(role);
}

I have made sure both user, and role is getting the correct user and correct role. Its also running the if statement because I had output inside of it. The only thing that doesn't seem to be working is the actual assignment. Any help is greatly appreciated. Thanks!

Solonce
  • 1
  • 1
  • 2

2 Answers2

5

If everything on the code side is already working, you might have a permissions issue on the Discord server itself.

I'll explain with an example:

Something similar happened to me a while back when trying to make some moderation commands that didn't work for the life of me, even though the code was correct and it gave no errors.

The issue turned out to be the role hierarchy on the discord server: basically the role the bot has assigned can only affect those underneath him, take for example the screenshot:

Discord role hierarchy

You'll notice there's an Inquisition role right underneath the Admin one.

That's a bot, which needs to be on top of every other role to be able to interact with those users, so I suggest you try and move your bot up the role ladder and see if that fixes your issue.

Cristian Moraru
  • 265
  • 4
  • 15
0

Hey Solonce your way of adding a role is almost correct but instead of your way of doing this using the ifs use the already added way to add new commands to your bot. I assume ! is your prefix and the stream is the command/word you want the bot to read and add the role accordingly. Let me know if your case is not what I assume.

So here is how it should ideally be done.

  1. Create a public class and make it inherit from ModuleBase<SocketCommandContext> so it looks like public class Matches : ModuleBase<SocketCommandContext>.
  2. Once you are done you can use [Command("stream")] to setup any commands you want for the bot.

And the way to add the roles is somewhat like this

[Command("stream")]
public async Task RoleTask()
{
    ulong roleId = 486599202093269012;
    var role = Context.Guild.GetRole(roleId);
    await ((SocketGuildUser) Context.User).AddRoleAsync(role);
}

On this the bot (if it has enough permissions) should grant the role with the roleId to the user who sends the command. ie !stream

Also you know if you are going to get started I'd really suggest you to have a look at some great documentation at http://docs.stillu.cc and another good way is to look at examples in order to learn. This is the bot I made way way back when I was getting started. Maybe you could learn.

Rishav
  • 3,818
  • 1
  • 31
  • 49