Discord.net 任务调用问题

问题描述

我正在尝试在 .NET 中创建一个 discord 机器人,并使用一个命令将用户放在标记上。例如,服务器管理员用户提供 50 个标记,该用户删除其所有角色,并获得一个名为“标记”的角色。然后,将出现一个文本通道,用户需要在其中发送 50 条垃圾邮件删除标记,然后他才能取回所有内容。我已经创建了用于提供标记删除它们的命令,但是当我尝试通过从 Program.cs 调用函数删除某人的标记时,它出于某种原因给出了一个空指针异常。代码如下:

Program.cs 事件:

private async Task MessageRecieved(SocketMessage arg)
        {
            //Checking if the user has markers
            var roles = (arg.Author as SocketGuildUser).Roles;
            bool imaMarkere = false;
            foreach (var role in roles)
            {
                if (role.Id == 844558286611152917)
                    imaMarkere = true;
            }
            
            if (arg.Channel.Id == 844558185024585770 && imaMarkere)
            {
                int markerCount = 0;
                using (sqliteConnection con = new sqliteConnection(connectionString))
                {
                    con.open();
                    string query = $"select markercount from markers";
                    var com = con.CreateCommand();
                    com.CommandText = query;
                    var rdr = com.ExecuteReader();
                    if (rdr.Read())
                    {
                        markerCount += Convert.ToInt32(rdr["markercount"]) - 1;
                    }
                    rdr.Close();
                    //Removing 1 marker per message recieved
                    if (markerCount > 0)
                    {
                        com.CommandText = $"update markers set markercount={markerCount} where userid='{arg.Author.Id}'";
                        com.ExecuteNonQuery();
                    }
                    //If no markers are left returning the user roles
                    else
                    {
                        Commands c = new Commands();
                        await c.Skini(arg.Author as IGuildUser);
                    }
                    con.Close();
                }
            }
            else return;
            
        }

命令.cs

[Command("skini"),RequireUserPermission(GuildPermission.Administrator,ErrorMessage = "Nemate potrebnu dozvolu ``ADMINISTRATOR``")]
            public async Task Skini(IGuildUser user = null)
            {
                if(user == null)
                {
                    await ReplyAsync($"{Context.User.Mention},kome da skinem?");
                }
            string rolesstring = "";
            string[] roles;
            using (sqliteConnection con = new sqliteConnection(connectionString))
            {
                con.open();
                var com = con.CreateCommand();
                string readQuery = "select pastroles from markers";
                com.CommandText = readQuery;
                var rdr = com.ExecuteReader();
                if (rdr.Read())
                {
                    rolesstring += rdr["pastroles"].ToString();
                }
                rdr.Close();
                string deleteQuery = $"delete from markers where userid = '{user.Id}'";
                com.CommandText = deleteQuery;
                com.ExecuteNonQuery();
                con.Close();
                
            }
            roles = rolesstring.Split(",");
            foreach (var roleid in roles)
            {
                var role = Context.Guild.GetRole(Convert.ToUInt64(roleid));//this line is where the exception happens
                if(!role.IsEveryone)
                    await user.AddRoleAsync(role);
            }
            var roleMarkeri = Context.Guild.GetRole(844558286611152917);
            await user.RemoveRoleAsync(roleMarkeri);
            var EmbedBuilder = new EmbedBuilder()
                    .WithTitle("**MARKERI**")
                    .WithColor(Color.Blue)
                    .WithDescription($":white_check_mark: {Context.User.Mention} je **skinuo** markere {user.Username}")
                    .WithFooter(footer =>
                    {
                        footer
                        .WithText("Marker log");
                    });
            Embed embed = EmbedBuilder.Build();
            await ReplyAsync(embed: embed);
        }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)