예제 #1
0
 // Static methods for serialization & deserialization.
 public static string ToString(MentionRole role)
 {
     return(role.type switch {
         Type.None => "none",
         Type.Here => "here",
         Type.Everyone => "everyone",
         Type.Discord => role.discord_role !.Id.ToString(),
         _ => throw new ArgumentException(),
     });
예제 #2
0
        // Static serialization/deserialization methods.
        public static BulletinData Parse(
            string command_option,
            string command_mention,
            string command_title,
            DiscordMessage message,
            Settings settings
            )
        {
            DiscordGuild guild = message.Channel.Guild;

            // Get DiscordRole to mention
            MentionRole?mention = null;

            if (command_mention == string.Empty)
            {
                command_mention = settings.default_mention?.Name()
                                  ?? Settings.mention_none;
            }
            if (command_mention != Settings.mention_none)
            {
                mention = MentionRole.FromName(command_mention, guild);
            }
            bool can_mention = Util.CanMention(
                mention,
                message.Author.ToDiscordMember(guild),
                settings.bulletin
                );

            if (!can_mention)
            {
                mention = null;
            }

            Group.Type group_type;
            if (command_option == "")
            {
                group_type = Group.default_type;
            }
            else
            {
                group_type = Group.ParseType(command_option);
            }

            // Instantiate BulletinData
            BulletinData data = new BulletinData(
                // DiscordUser -> DiscordMember is guaranteed to succeed here
                message.Author.ToDiscordMember(guild) !,
                command_title,
                mention,
                message.Timestamp + settings.duration,
                new Group(group_type)
                );

            return(data);
        }
예제 #3
0
        // Serialization / deserialization of a list of Settings.
        public static async Task <Dictionary <ulong, Settings> > Import(
            string path,
            DiscordClient client
            )
        {
            Dictionary <ulong, Settings> dict = new Dictionary <ulong, Settings>();

            log.Info("Importing settings: " + path);
            StreamReader file;

            try {
                file = new StreamReader(path);
            } catch (Exception) {
                log.Error("Could not open \"" + path + "\".", 1);
                log.Error("No previously saved settings loaded.", 1);
                return(dict);
            }

            while (!file.EndOfStream)
            {
                string line = file.ReadLine() ?? "";

                Settings     settings = new Settings(null);
                ulong        guild_id = Convert.ToUInt64(line);
                DiscordGuild guild;
                try {
                    guild = await client.GetGuildAsync(guild_id);

                    log.Info("Server: " + guild.Name, 1);
                } catch (UnauthorizedException) {
                    log.Error("Not authorized to access guild: " + guild_id.ToString(), 1);
                    for (int i = 0; i < count_keys; i++)
                    {
                        file.ReadLine();                                // discard
                    }
                    continue;
                }

                Dictionary <string, string> lines = new Dictionary <string, string>();
                for (int i = 0; i < count_keys; i++)
                {
                    line = file.ReadLine() ?? "";
                    string[] line_parts = line.Split(key_separator, 2);
                    lines.Add(line_parts[0], line_parts[1]);
                }

                foreach (string key in lines.Keys)
                {
                    string data = lines[key];
                    switch (key)
                    {
                    case key_bulletin:
                        if (data == "null")
                        {
                            settings.bulletin = null;
                            break;
                        }
                        ulong bulletin_id = Convert.ToUInt64(data);
                        settings.bulletin = await client.GetChannelAsync(bulletin_id);

                        break;

                    case key_mention:
                        if (data == "null")
                        {
                            settings.default_mention = null;
                            break;
                        }
                        settings.default_mention = MentionRole.FromID(data, guild);
                        break;

                    case key_duration:
                        int duration_min = Convert.ToInt32(data);
                        settings.duration = TimeSpan.FromMinutes(duration_min);
                        break;

                    case key_increment:
                        int increment_min = Convert.ToInt32(data);
                        settings.increment = TimeSpan.FromMinutes(increment_min);
                        break;
                    }
                }

                dict.Add(guild_id, settings);
            }
            file.Close();
            log.Info("Settings import complete.");

            return(dict);
        }
예제 #4
0
 public static string Rolestring(this MentionRole r)
 {
     return("@" + r.Name());
 }