Esempio n. 1
0
        public async Task CallTransactionMadeEventAsync(IDiscordMessageChannel m, User receiver, User giver, int amount)
        {
            try
            {
                TransactionPacket p = new TransactionPacket();
                p.discordChannel = m;
                p.discordUser    = new RuntimeUser(Bot.instance.Client.GetUser(receiver.Id.FromDbLong()));

                if (giver != null)
                {
                    p.giver = giver;
                }

                p.receiver = receiver;

                p.amount = amount;

                if (OnTransaction != null)
                {
                    await OnTransaction?.Invoke(p);
                }
            }
            catch (Exception e)
            {
                await MeruUtils.ReportErrorAsync(e);

                Log.WarningAt("achievement check failed", e.ToString());
            }
        }
Esempio n. 2
0
        public async Task CalculateAsync(EventContext e)
        {
            Locale locale = Locale.GetEntity(e.Channel.Id.ToDbLong());

            await MeruUtils.TryAsync(async() =>
            {
                Expression expression = new Expression(e.arguments);

                expression.Parameters.Add("pi", Math.PI);

                expression.EvaluateFunction += (name, x) =>
                {
                    if (name == "lerp")
                    {
                        double n = (double)x.Parameters[0].Evaluate();
                        double v = (double)x.Parameters[1].Evaluate();
                        double o = (double)x.Parameters[2].Evaluate();
                        x.Result = (n * (1.0 - o)) + (v *o);
                    }
                };

                object output = expression.Evaluate();

                await e.Channel.SendMessage(output.ToString());
            },
                                     async (ex) =>
            {
                Log.ErrorAt("calc", ex.Message);
                await e.Channel.SendMessage(locale.GetString("miki_module_general_calc_error") + "\n```" + ex.Message + "```");
            });
        }
Esempio n. 3
0
 private async Task Module_MessageReceived(IDiscordMessage message)
 {
     if (await IsEnabled(message.Guild.Id))
     {
         await MeruUtils.TryAsync(async() =>
         {
             Task.Run(() => MessageRecieved(message));
         });
     }
 }
Esempio n. 4
0
 private async Task Module_UserLeft(IDiscordUser arg)
 {
     if (await IsEnabled(arg.Guild.Id))
     {
         await MeruUtils.TryAsync(async() =>
         {
             await UserLeaveGuild(arg.Guild, arg);
         });
     }
 }
Esempio n. 5
0
 private async Task Module_LeftGuild(IDiscordGuild arg)
 {
     if (await IsEnabled(arg.Id))
     {
         await MeruUtils.TryAsync(async() =>
         {
             await LeftGuild(arg);
         });
     }
 }
Esempio n. 6
0
 private async Task Module_UserUpdated(IDiscordUser arg1, IDiscordUser arg2)
 {
     if (arg1.Guild != null)
     {
         if (await IsEnabled(arg1.Guild.Id))
         {
             await MeruUtils.TryAsync(async() =>
             {
                 await UserUpdated(arg1, arg2);
             });
         }
     }
 }
Esempio n. 7
0
 internal async Task OnCommandDone(IDiscordMessage e, ICommandEvent commandEvent, bool success = true)
 {
     foreach (CommandDoneEvent ev in Events.CommandDoneEvents.Values)
     {
         MeruUtils.TryAsync(async() =>
         {
             await ev.processEvent(e, commandEvent, success);
         },
                            async(ex) =>
         {
             Log.ErrorAt($"commanddone@{ev.Name}", ex.Message);
         });
     }
 }
Esempio n. 8
0
        public async Task <IDiscordMessage> SendMessage(string message)
        {
            RuntimeMessage m = null;

            MeruUtils.TryAsync(async() =>
            {
                m = new RuntimeMessage(await(channel as IMessageChannel).SendMessage(message));
                Log.Message("Sent message to channel " + channel.Name);
            },
                               async(ex) =>
            {
                Log.ErrorAt("SendMessage", "failed to send");
            });
            return(m);
        }
Esempio n. 9
0
        private async Task InternalMessageReceived(IDiscordMessage message)
        {
            await MeruUtils.TryAsync(async() =>
            {
                await Task.Run(() => OnMessageRecieved(message));

                if (message.MentionedUserIds.Contains(Bot.instance.Client.CurrentUser.Id))
                {
                    await OnMention(message);
                }
            },
                                     async (e) =>
            {
                Log.ErrorAt("messagerecieved", e.ToString());
            });
        }
Esempio n. 10
0
        private async Task <bool> TryProcessCommand(ProcessCommandDelegate cmd, EventContext context)
        {
            bool success = false;
            await MeruUtils.TryAsync(async() =>
            {
                Log.Message($"Do we get here? {Name}");
                await cmd(context);
                success = true;
            },
                                     async (ex) =>
            {
                Log.ErrorAt(Name, ex.Message + "\n" + ex.StackTrace);
                await eventSystem.OnCommandError(ex, this, context.message);
            });

            return(success);
        }
Esempio n. 11
0
        public async Task <IDiscordMessage> SendMessage(IDiscordEmbed embed)
        {
            RuntimeMessage m = null;
            await MeruUtils.TryAsync(async() =>
            {
                Log.Message("Sent embed to channel " + channel.Name);
                m = new RuntimeMessage(
                    await(channel as IMessageChannel)
                    .SendMessageAsync("", false, (embed as IProxy <EmbedBuilder>)
                                      .ToNativeObject()));
            },
                                     async (ex) =>
            {
                Log.ErrorAt("SendMessage", ex.Message);
            });

            return(m);
        }
Esempio n. 12
0
        /// <summary>
        /// Loads addons in ./modules folder
        /// </summary>
        public async Task Load(Bot bot)
        {
            if (!Directory.Exists(CurrentDirectory) || Directory.GetFiles(CurrentDirectory).Length == 0)
            {
                Log.Warning("No modules found, ignoring...");
                Directory.CreateDirectory(CurrentDirectory);
                return;
            }

            string[] allFiles = Directory.GetFiles(CurrentDirectory);

            foreach (string s in allFiles)
            {
                string newS = s.Split('/')[s.Split('/').Length - 1];
                newS = newS.Remove(newS.Length - 4);

                try
                {
                    if (!s.EndsWith(".dll"))
                    {
                        continue;
                    }

                    Assembly addon = Assembly.Load(File.ReadAllBytes(s));

                    if (addon.CreateInstance(newS + ".Addon") is IAddon currentAddon)
                    {
                        IAddonInstance aInstance = new RuntimeAddonInstance();
                        aInstance = await currentAddon.Create(aInstance);

                        foreach (IModule nm in aInstance.Modules)
                        {
                            IModule newModule = new RuntimeModule(nm);
                            await newModule.InstallAsync(bot);
                        }
                        Log.Done($"loaded Add-On {newS} successfully");
                    }
                }
                catch (Exception ex)
                {
                    await MeruUtils.ReportErrorAsync(ex);
                }
            }
        }
Esempio n. 13
0
        public async Task CheckAsync(IDiscordMessage e)
        {
            if (e.Author.IsBot)
            {
                return;
            }

            if (!lastTimeExpGranted.ContainsKey(e.Author.Id))
            {
                lastTimeExpGranted.Add(e.Author.Id, DateTime.MinValue);
            }

            if (lastTimeExpGranted[e.Author.Id].AddMinutes(1) < DateTime.Now)
            {
                int addedExperience = MikiRandom.Next(2, 5);

                await MeruUtils.TryAsync(async() =>
                {
                    User a;
                    LocalExperience experience;

                    long userId = e.Author.Id.ToDbLong();

                    int currentGlobalLevel = 0;
                    int currentLocalLevel  = 0;

                    using (var context = new MikiContext())
                    {
                        a = await context.Users.FindAsync(e.Author.Id.ToDbLong());

                        if (a == null)
                        {
                            a = await User.CreateAsync(e);
                        }

                        experience = await context.Experience.FindAsync(e.Guild.Id.ToDbLong(), userId);

                        if (experience == null)
                        {
                            experience = await LocalExperience.CreateAsync(context, e.Guild.Id.ToDbLong(), e.Author.Id.ToDbLong());
                        }

                        if (experience.LastExperienceTime == null)
                        {
                            experience.LastExperienceTime = DateTime.Now;
                        }

                        GuildUser guildUser = await context.GuildUsers.FindAsync(e.Guild.Id.ToDbLong());
                        if (guildUser == null)
                        {
                            long guildId  = e.Guild.Id.ToDbLong();
                            int?userCount = Bot.instance.Client.GetGuild(e.Guild.Id).Users.Count;

                            int?value = await context.Experience
                                        .Where(x => x.ServerId == guildId)
                                        .SumAsync(x => x.Experience);

                            guildUser                  = new GuildUser();
                            guildUser.Name             = e.Guild.Name;
                            guildUser.Id               = guildId;
                            guildUser.Experience       = value ?? 0;
                            guildUser.UserCount        = userCount ?? 0;
                            guildUser.LastRivalRenewed = Utils.MinDbValue;
                            guildUser.MinimalExperienceToGetRewards = 100;

                            guildUser = context.GuildUsers.Add(guildUser);
                        }

                        currentLocalLevel  = User.CalculateLevel(experience.Experience);
                        currentGlobalLevel = User.CalculateLevel(a.Total_Experience);

                        experience.Experience += addedExperience;
                        a.Total_Experience    += addedExperience;
                        guildUser.Experience  += addedExperience;

                        await context.SaveChangesAsync();
                    }


                    if (currentLocalLevel != User.CalculateLevel(experience.Experience))
                    {
                        await LevelUpLocalAsync(e, a, currentLocalLevel + 1);
                    }

                    if (currentGlobalLevel != User.CalculateLevel(a.Total_Experience))
                    {
                        await LevelUpGlobalAsync(e, a, currentGlobalLevel + 1);
                    }

                    lastTimeExpGranted[e.Author.Id] = DateTime.Now;
                });
            }
        }