Example #1
0
        public Task <ActionResult> LeavingMessageAsync([Remainder] string message = null)
        {
            if (message is null)
            {
                return(Ok(new StringBuilder()
                          .AppendLine("The current leaving message for this guild is ```")
                          .AppendLine(Context.GuildData.Configuration.Welcome.LeavingMessage)
                          .Append("```")
                          .ToString()));
            }

            Context.GuildData.Configuration.Welcome.LeavingMessage = message;
            Db.UpdateData(Context.GuildData);
            var welcomeChannel = Context.Guild.GetTextChannel(Context.GuildData.Configuration.Welcome.WelcomeChannel);
            var sendingTest    = Context.GuildData.Configuration.Welcome.WelcomeChannel == 0 || welcomeChannel is null
                    ? "Not sending a test message, as you do not have a welcome channel set. " +
                                 "Set a welcome channel to fully complete the setup!"
                    : $"Sending a test message to {welcomeChannel.Mention}.";

            if (welcomeChannel is null || Context.GuildData.Configuration.Welcome.WelcomeChannel is 0)
            {
                return(None());
            }

            return(Ok(new StringBuilder()
                      .AppendLine($"Set this server's leaving message to ```{message}```")
                      .AppendLine()
                      .AppendLine($"{sendingTest}").ToString(),
                      _ => WelcomeService.LeaveAsync(new UserLeftEventArgs(Context.User))));
        }
        public async Task BotShouldNotProcessEventOutsideTimeout()
        {
            var config = new AppSettings {
                ProcessEventTimeout = TimeSpan.FromMinutes(5.0)
            };
            var welcomeService = new WelcomeService(config, _usersStore, _logger, _botMock.Object);

            const int testUserId = 345;

            await ProcessNewChatMember(welcomeService, testUserId, DateTime.UtcNow - TimeSpan.FromMinutes(6.0));

            Assert.Empty(_botMock.Invocations);
            Assert.Empty(_usersStore.GetAll());
        }
Example #3
0
        public async Task welcome([RemainderAttribute] string prefix)
        {
            var activeuser  = Context.User as IGuildUser;
            var application = await Context.Client.GetApplicationInfoAsync();

            if (activeuser.GuildPermissions.Has(GuildPermission.Administrator) || activeuser.Id == application.Owner.Id)
            {
                WelcomeService.addWelcomes(Context.Guild, prefix);
                Justibot.Saver.SaveWelcomes(activeuser, prefix);
                await ReplyAsync($"{Context.User.Mention}, set the welcome message to: \n{prefix}");
            }
            else
            {
                await ReplyAsync("You do not have permission to use this command");
            }
        }
        public async Task BotShouldProcessEventWithinTimeout()
        {
            var config = new AppSettings {
                ProcessEventTimeout = TimeSpan.FromSeconds(5.0)
            };
            var welcomeService = new WelcomeService(config, _usersStore, _logger, _botMock.Object);

            const int testUserId = 123;

            await ProcessNewChatMember(welcomeService, testUserId, DateTime.UtcNow);

            Assert.Collection(_botMock.Invocations,
                              getChatMember => Assert.Equal(nameof(ITelegramBotClient.GetChatMemberAsync), getChatMember.Method.Name),
                              restrict => Assert.Equal(nameof(ITelegramBotClient.RestrictChatMemberAsync), restrict.Method.Name),
                              sendMessage => Assert.Equal(nameof(ITelegramBotClient.SendTextMessageAsync), sendMessage.Method.Name));

            Assert.Equal(testUserId, _usersStore.GetAll().Single().Id);
        }
        private static Task ProcessNewChatMember(WelcomeService service, int userId, DateTime enterTime, int fromId = 0)
        {
            var testUser = new User {
                Id = userId
            };
            var message = new Message
            {
                Date = enterTime,
                Chat = new Chat(),
                From = new User {
                    Id = fromId
                },
                NewChatMembers = new[]
                {
                    testUser
                }
            };

            return(service.ProcessNewChatMember(message));
        }
        public async Task BotShouldRestrictTheEnteringUserAndNotTheMessageAuthor()
        {
            var config         = new AppSettings();
            var welcomeService = new WelcomeService(config, _usersStore, _logger, _botMock.Object);

            const int enteringUserId = 123;
            const int invitingUserId = 345;

            await ProcessNewChatMember(welcomeService, enteringUserId, DateTime.UtcNow, invitingUserId);

            Assert.Collection(_botMock.Invocations,
                              _ => {},
                              restrict =>
            {
                Assert.Equal(nameof(ITelegramBotClient.RestrictChatMemberAsync), restrict.Method.Name);
                var restrictedUserId = (int)restrict.Arguments[1];
                Assert.Equal(enteringUserId, restrictedUserId);
            },
                              _ => {});
        }
Example #7
0
        public async Task Start()
        {
            Configuration.configure();

            //create client and add what serverity of information to log
            client = new DiscordSocketClient(new DiscordSocketConfig()
            {
                LogLevel            = LogSeverity.Info,
                ExclusiveBulkDelete = false,
            });

            //create template for logged messages
            client.Log += (message) =>
            {
                Console.WriteLine(message.ToString());
                return(Task.CompletedTask);
            };

            //when client is ready, load important information to memory for quick access and set game to help message
            client.Ready += async() =>
            {
                await client.SetGameAsync("Use +help");

                PrefixService.LoadPrefixs(client);
                WelcomeService.LoadWelcomes(client);
                LeavingService.Loadleaving(client);
                XpService.Loadxps(client);

                //start timer for xp module
                XpService.timerStart();

                //if version from data file has changed, reset update time (used to track time since update)
                if (Justibot.Loader.checkUpdate().Value != Justibot.Data.version)
                {
                    Justibot.Saver.addVersion(DateTime.Now, Justibot.Data.version);
                }
            };

            //log the client into discord with the bot token
            await client.LoginAsync(TokenType.Bot, Configuration.config.Token);

            await client.StartAsync();

            //when a user joins a guild, check appropriate permissions and react accordingly
            client.UserJoined += async(user) =>
            {
                //check welcome module
                var results = Justibot.Loader.LoadPerm(user, "WELCOME");
                var guild   = user.Guild as IGuild;
                //if welcome module enabled send message
                if (results.Item1 == true)
                {
                    ulong result2 = results.Item2;
                    //get designated channel for welcome messages
                    SocketTextChannel welcomeChannel = user.Guild.GetChannel(result2) as SocketTextChannel;

                    //if welcome message is available for guild use it, otherwise use default
                    bool check = welcomedict.welcomes.TryGetValue(guild.Id, out string welcome);
                    if (!check)
                    {
                        welcome = "Welcome, **[user]** has joined **[server]!!!** \n" +
                                  "Have a good time!!!";
                    }
                    //fill in placeholder text with appropriate information
                    welcome = welcome.Replace("[user]", user.Username);
                    welcome = welcome.Replace("[server]", guild.Name);

                    //if permission argument is plain send message in plain text otherwise send in embeded message
                    if (results.Item3 == "PLAIN")
                    {
                        await welcomeChannel.SendMessageAsync(welcome);
                    }
                    else
                    {
                        string avatar = user.GetAvatarUrl();
                        if (avatar == null)
                        {
                            avatar = "https://cdn.discordapp.com/embed/avatars/0.png";
                        }
                        Color color = new Color(0, 255, 0);
                        if (avatar.Contains("/a_"))
                        {
                            avatar = $"{avatar.Remove(avatar.Length - 12)}gif?size=128";
                        }

                        var embed = new EmbedBuilder()
                                    .WithColor(color)
                                    .WithTitle($"{Format.Bold($"{client.CurrentUser.Username}:")}")
                                    .WithDescription(welcome)
                                    .WithThumbnailUrl(avatar)
                                    .Build();

                        await welcomeChannel.SendMessageAsync(user.Mention, false, embed);
                    }
                }
                //if joining role is enabled give new user the role
                var joinRoles = Justibot.Loader.LoadPerm(user, "JOINROLE");
                if (joinRoles.Item1 == true)
                {
                    IRole joinerRole = user.Guild.GetRole(joinRoles.Item2) as IRole;
                    await user.AddRoleAsync(joinerRole);
                }
                //if log permission enabled send message to log channel
                var results2 = Justibot.Loader.LoadPerm(user, "LOG");
                if (results2.Item1 == true)
                {
                    ulong             result2    = results2.Item2;
                    SocketTextChannel logChannel = user.Guild.GetChannel(result2) as SocketTextChannel;

                    if (results2.Item3 == "PLAIN")
                    {
                        await logChannel.SendMessageAsync($"{Format.Bold($"{user.Username}")}#{user.Discriminator}, ID:<{user.Id}> has joined the server. \n" +
                                                          $"There are now {user.Guild.MemberCount} members.");
                    }
                    else
                    {
                        string avatar = user.GetAvatarUrl();
                        Color  color  = new Color(0, 255, 0);
                        //if no avatar available use deafult
                        if (avatar == null)
                        {
                            avatar = "https://cdn.discordapp.com/embed/avatars/0.png";
                        }
                        //check if avatar is gif (gif avatars contain /a_ in their URLs
                        if (avatar.Contains("/a_"))
                        {
                            avatar = $"{avatar.Remove(avatar.Length - 12)}gif?size=128";
                        }

                        var embed = new EmbedBuilder()
                                    .WithColor(color)
                                    .WithTitle($"{Format.Bold($"{client.CurrentUser.Username}:")}")
                                    .WithDescription($"{Format.Bold($"{user.Username}")}#{user.Discriminator}, ID:<{user.Id}> has joined the server. \n" +
                                                     $"There are now {user.Guild.MemberCount} members.")
                                    .WithThumbnailUrl(avatar)
                                    .Build();

                        await logChannel.SendMessageAsync("", false, embed);
                    }
                }
                //if bunker mode is enabled on server instantly kick new user
                if (Settings.bunker.Contains(guild.Id))
                {
                    await user.Guild.DefaultChannel.SendMessageAsync($"{Format.Bold($"{user.Username}")} was kicked by bunker mode!");

                    await user.KickAsync("Bunker mode enabled");
                }
            };

            //when user leaves guild check appropriate permissions and act accordingly
            client.UserLeft += async(user) =>
            {
                //check leaving permission
                var results = Justibot.Loader.LoadPerm(user, "LEAVING");
                if (results.Item1 == true)
                {
                    ulong result2 = results.Item2;
                    var   guild   = user.Guild as IGuild;
                    //get channel to send leaving message to
                    SocketTextChannel leaveChannel = user.Guild.GetChannel(result2) as SocketTextChannel;

                    //if leaving message available set it, else use default message
                    bool check = welcomedict.leaves.TryGetValue(guild.Id, out string leave);
                    if (!check)
                    {
                        leave = "**[mention]** has left **[server]**, goodbye.";
                    }
                    //replace placeholder text with appropriate items
                    leave = leave.Replace("[user]", user.Username);
                    leave = leave.Replace("[mention]", user.Mention);
                    leave = leave.Replace("[server]", guild.Name);

                    //if leaving permission argument is plain, send plain text otherwise send as an embed message
                    if (results.Item3 == "PLAIN")
                    {
                        await leaveChannel.SendMessageAsync(leave);
                    }
                    else
                    {
                        string avatar = user.GetAvatarUrl();
                        Color  color  = new Color(255, 0, 0);
                        //if no avatar available use deafult
                        if (avatar == null)
                        {
                            avatar = "https://cdn.discordapp.com/embed/avatars/0.png";
                        }
                        //check if avatar is gif (gif avatars contain /a_ in their URLs
                        if (avatar.Contains("/a_"))
                        {
                            avatar = $"{avatar.Remove(avatar.Length - 12)}gif?size=128";
                        }

                        var embed = new EmbedBuilder()
                                    .WithColor(color)
                                    .WithTitle($"{Format.Bold($"{client.CurrentUser.Username}:")}")
                                    .WithDescription(leave)
                                    .WithThumbnailUrl(avatar)
                                    .Build();

                        await leaveChannel.SendMessageAsync("", false, embed);
                    }
                }

                //if log module is enabled, send message to log channel bassed on argument (plain text or embed)
                var results2 = Justibot.Loader.LoadPerm(user, "LOG");
                if (results2.Item1 == true)
                {
                    ulong             result2        = results2.Item2;
                    var               guild          = user.Guild as IGuild;
                    SocketTextChannel welcomeChannel = user.Guild.GetChannel(result2) as SocketTextChannel;

                    if (results2.Item3 == "PLAIN")
                    {
                        await welcomeChannel.SendMessageAsync($"{Format.Bold($"{user.Username}")}#{user.Discriminator}, ID:<{user.Id}> has left the server. \n" +
                                                              $"There are now {user.Guild.MemberCount} members.");
                    }
                    else
                    {
                        string avatar = user.GetAvatarUrl();
                        Color  color  = new Color(255, 0, 0);
                        //if no avatar available use deafult
                        if (avatar == null)
                        {
                            avatar = "https://cdn.discordapp.com/embed/avatars/0.png";
                        }
                        //check if avatar is gif (gif avatars contain /a_ in their URLs
                        if (avatar.Contains("/a_"))
                        {
                            avatar = $"{avatar.Remove(avatar.Length - 12)}gif?size=128";
                        }

                        var embed = new EmbedBuilder()
                                    .WithColor(color)
                                    .WithTitle($"{Format.Bold($"{client.CurrentUser.Username}:")}")
                                    .WithDescription($"{Format.Bold($"{user.Username}")}#{user.Discriminator}, ID:<{user.Id}> has left the server. \n" +
                                                     $"There are now {user.Guild.MemberCount} members.")
                                    .WithThumbnailUrl(avatar)
                                    .Build();

                        await welcomeChannel.SendMessageAsync("", false, embed);
                    }
                }
            };

            //if user messages a channel the bot can see, check permissions
            client.MessageReceived += async(message) =>
            {
                var guild = (message.Channel as SocketTextChannel)?.Guild;
                var user  = (message.Author as IGuildUser);
                if (guild != null)
                {
                    //if user isnt bot, give xp to user
                    if (user.IsBot == false)
                    {
                        var guild2 = guild as IGuild;
                        XpService.AddXp(user, guild2);
                    }
                    //if message contains a discord invite link, check if use has manage guild permissions
                    if ((message.ToString().ToUpper()).Contains("DISCORD.GG/") && (!(user.IsBot)))
                    {
                        //if user does not have manage guild permission check if advertising module is enabled
                        if (!(user.GuildPermissions.Has(GuildPermission.ManageGuild)) || !(Justibot.Loader.isStaff(user)))
                        {
                            var results = Justibot.Loader.LoadPerm(user, "ADVERTISING");
                            if (results.Item1 == true)
                            {
                                //if module is set to delete, remove message and asks user to not advertise in the server
                                ulong result2 = results.Item2;
                                if (results.Item3 == "DELETE")
                                {
                                    await message.DeleteAsync();

                                    var response = await message.Channel.SendMessageAsync($"{user.Mention} please do not advertise in this server");

                                    respond(response);
                                }
                                //if module is set to repost, send copy of message to advertising channel and ask user to only post advertisements to channel
                                else if (results.Item3 == "REPOST")
                                {
                                    SocketTextChannel advertisingChannel = guild.GetChannel(result2) as SocketTextChannel;
                                    if (message.Channel != advertisingChannel)
                                    {
                                        await advertisingChannel.SendMessageAsync($"{user.Mention} advertised message: \n{message.ToString()}");

                                        await message.DeleteAsync();

                                        var response = await message.Channel.SendMessageAsync($"{user.Mention} please keep advertising to {advertisingChannel.Mention}, your message has been shifted there.");

                                        respond(response);
                                    }
                                }
                            }
                        }
                    }
                }
            };
            //initialize commands and commandhandler
            handler = new CommandHandler();
            await handler.InitCommands(client);

            //prevent application shutdown
            await Task.Delay(-1);
        }
Example #8
0
 public WelcomeHandler(DiscordShardedClient client, LogsService logs, CommandService command, WelcomeService welcome) : base(client, logs)
 {
     _command = command;
     _welcome = welcome;
 }
Example #9
0
 public WelcomeMessageModule(WelcomeService welcome, IDbLanguage language, LocalizationService localization)
 {
     _welcome      = welcome;
     _language     = language;
     _localization = localization;
 }