Discord.NET assigning roles

时光怂恿深爱的人放手 提交于 2020-12-15 07:13:20

问题


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!


回答1:


If everything on the code side is already working, you might have a permissions issue on the Discord server itself, happened to me once when trying to make some moderation happen and it didn't work for the life of me. Turns out the issue was simply the role hierarchy on the discord server, basically the role the bot is in can only affect those underneath him, take for example the screenshot:

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.




回答2:


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.



来源:https://stackoverflow.com/questions/52153990/discord-net-assigning-roles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!