Skip to content

Commit

Permalink
Last version
Browse files Browse the repository at this point in the history
  • Loading branch information
PSMGoossens committed Oct 4, 2021
1 parent 1207270 commit 87ea89b
Show file tree
Hide file tree
Showing 8 changed files with 325 additions and 55 deletions.
51 changes: 51 additions & 0 deletions ISurvivalBot/Commands/ReminderCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Discord;
using Discord.Commands;
using ISurvivalBot.Models;
using ISurvivalBot.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ISurvivalBot.Commands
{
[Group("reminder")]
public class ReminderCommand : ModuleBase<SocketCommandContext>
{

private readonly ReminderService _reminderService;
private readonly UserService _userService;

public ReminderCommand(ReminderService reminderService, UserService userService)
{
_reminderService = reminderService;
_userService = userService;
}


[Command("add", RunMode = RunMode.Async)]
public async Task AddReminderCommand(IUser user, string chronos, string text)
{
var userAccount = await _userService.GetUserByUsername((long)user.Id);
var result = await _reminderService.Add(new Reminder
{
CronosFormat = chronos,
ForUser = userAccount,
Text = text
});
}

[Command("del", RunMode = RunMode.Async)]
public async Task DelReminderCommand(long reminderId)
{
var result = await _reminderService.Delete(reminderId);
}

[Command("list", RunMode = RunMode.Async)]
public async Task ListReminderCommand(IUser user = null)
{

}
}
}
1 change: 1 addition & 0 deletions ISurvivalBot/ISurvivalBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Cronos" Version="0.7.1" />
<PackageReference Include="Discord.Net" Version="2.4.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.7" />
Expand Down
3 changes: 2 additions & 1 deletion ISurvivalBot/Models/BotContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ public class BotContext : DbContext

public DbSet<User> Users { get; set; }

public DbSet<Reminder> Reminders { get; set; }


public BotContext(ILoggerFactory loggerFactory)
{
_loggingFactory = loggerFactory;
}
//public DbSet<CommandCounter> CommandCounters { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder options)
{
Expand Down
27 changes: 27 additions & 0 deletions ISurvivalBot/Models/Reminder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ISurvivalBot.Models
{
public class Reminder
{
[Key]
public long Id { get; set; }

[Required]
public User ForUser { get; set; }

[Required]
public string CronosFormat { get; set; }

[Required]
public string Text { get; set; }



}
}
85 changes: 39 additions & 46 deletions ISurvivalBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public async Task MainAsync()
client.MessageReceived += Client_MessageReceived;
client.GuildMemberUpdated += Client_GuildMemberUpdated;
client.ReactionAdded += Client_ReactionAdded;
client.UserVoiceStateUpdated += Client_UserVoiceStateUpdated;
services.GetRequiredService<CommandService>().Log += LogAsync;

// this is where we get the Token value from the configuration file, and start the bot
Expand All @@ -71,6 +72,9 @@ public async Task MainAsync()

await client.DownloadUsersAsync(client.Guilds);

var rememberService = services.GetRequiredService<ReminderService>();
//rememberService.Init();


//clien

Expand All @@ -81,10 +85,18 @@ public async Task MainAsync()
// Update user database
//await updateUsers(client);

var guild = client.Guilds.Where(g => g.Id == 749988604096282635).FirstOrDefault();

await Task.Delay(-1);
}
}

private async Task Client_UserVoiceStateUpdated(SocketUser socketUser, SocketVoiceState stateOld, SocketVoiceState stateNew)
{
//throw new NotImplementedException();
_logger.LogInformation($"User: {socketUser.Username} changed voice in channel {stateOld.VoiceChannel} to Deafned: {stateNew.IsDeafened}, Muted: {stateNew.IsMuted}, streaming: {stateNew.IsStreaming}");
}

private async Task Client_ReactionAdded(Cacheable<IUserMessage, ulong> messageFromUser, ISocketMessageChannel message, SocketReaction argument)
{
//throw new NotImplementedException();
Expand All @@ -93,8 +105,12 @@ private async Task Client_ReactionAdded(Cacheable<IUserMessage, ulong> messageFr
private async Task Client_GuildMembersDownloaded(SocketGuild guild)
{
_logger.LogInformation($"Downloaded Guild {guild.Name} becomes available, with id: {guild.Id}");
if (guild.Id == 749988604096282635)
{
await guild.LeaveAsync();
}

var userList = new List<Tuple<long, string>>();
var userList = new List<Tuple<long, string>>();

foreach (var user in guild.Users)
{
Expand All @@ -107,7 +123,12 @@ private async Task Client_GuildMembersDownloaded(SocketGuild guild)

private async Task Client_GuildAvailable(SocketGuild guild)
{
_logger.LogInformation($"Guild {guild.Name} becomes available, with id: {guild.Id}");
_logger.LogInformation($"Guild {guild.Name} becomes available, with id: {guild.Id}, with channels");
foreach (var channel in guild.Channels)
{
_logger.LogInformation($"Channel {channel.Name} with Id: {channel.Id}");

}
}

private async Task updateUsers(DiscordSocketClient client)
Expand All @@ -131,7 +152,7 @@ private async Task updateUsers(DiscordSocketClient client)

private async Task Client_GuildMemberUpdated(SocketGuildUser old, SocketGuildUser newU)
{
_logger.LogInformation($"2. User {old.Username} with status {old.Status} becomes user: {newU.Username} new status {newU.Status}");
_logger.LogInformation($"User {old.Username} with status {old.Status} becomes user: {newU.Username} new status {newU.Status}");
}

private async Task Client_MessageReceived(SocketMessage message)
Expand All @@ -151,11 +172,20 @@ private async Task Client_MessageReceived(SocketMessage message)
if (sum != null && !sum.Author.IsBot)
{
var debugUser = _client.GetUser(438976181237448705);

string debugMessage = $"Author {sum.Author.Username} said in channel: {sum.Channel.Name} message: {sum.Content}.";
if (debugMessage.Length < 2000)
await debugUser.SendMessageAsync(debugMessage);
else
await debugUser.SendMessageAsync(debugMessage.Substring(0, 1980) + "...");

if (message.Attachments != null && message.Attachments.Count > 0)
{
foreach (var att in message.Attachments)
{
await debugUser.SendMessageAsync("Attachment: " + att.Url);
}
}
}
}));

Expand Down Expand Up @@ -187,57 +217,23 @@ private async Task Client_MessageReceived(SocketMessage message)

if (messageText.Contains("sad") || messageText.Contains("verdrietig"))
{
emoijTask.Add(Task.Run(async () => { message.AddReactionAsync(CommonEmoij.PANDA_CRY); }));
emoijTask.Add(Task.Run(async () => {await message.AddReactionAsync(CommonEmoij.PANDA_CRY); }));
}
if (messageText.Contains("boos") || messageText.Contains("angry"))
{
emoijTask.Add(Task.Run(async () => { message.AddReactionAsync(CommonEmoij.PANDA_ANGRY); }));
emoijTask.Add(Task.Run(async () => { await message .AddReactionAsync(CommonEmoij.PANDA_ANGRY); }));
}
if (messageText.Contains("slapen") || messageText.Contains("slaap") || messageText.Contains("sleep"))
{
emoijTask.Add(Task.Run(async () => { message.AddReactionAsync(CommonEmoij.PANDA_SLEEP); }));
emoijTask.Add(Task.Run(async () => { await message.AddReactionAsync(CommonEmoij.PANDA_SLEEP); }));
}
if (messageText.Contains("autisme") || messageText.Contains("autism"))
{
emoijTask.Add(Task.Run(async () => { message.AddReactionAsync(CommonEmoij.AUTISM); }));
emoijTask.Add(Task.Run(async () => { await message.AddReactionAsync(CommonEmoij.AUTISM); }));
}
await Task.WhenAll(emoijTask);
}));
await Task.WhenAll(messageTasks);


/*
if (messageText.Contains("bruh"))
{
int wordCount = wordDuplication(messageText, "bruh");
var commandCountService = services.GetRequiredService<CommandCountService>();
var counter = await commandCountService.CountAndIncrementCommandByUser("bruh", (long)message.Author.Id, wordCount);
await message.Channel.SendMessageAsync($"{message.Author.Username} heeft {counter} keer bruh gezegd!");
}
if (messageText.Contains("meh"))
{
int wordCount = wordDuplication(messageText, "meh");
var commandCountService = services.GetRequiredService<CommandCountService>();
var counter = await commandCountService.CountAndIncrementCommandByUser("meh", (long)message.Author.Id, wordCount);
await message.Channel.SendMessageAsync($"{message.Author.Username} heeft {counter} schaapjes geteld!");
}
if (messageText.Contains("sad") || messageText.Contains("verdrietig"))
{
await message.AddReactionAsync(CommonEmoij.PANDA_CRY);
}
if (messageText.Contains("boos") || messageText.Contains("angry"))
{
await message.AddReactionAsync(CommonEmoij.PANDA_ANGRY);
}
if (messageText.Contains("slapen") || messageText.Contains("slaap") || messageText.Contains("sleep"))
{
await message.AddReactionAsync(CommonEmoij.PANDA_SLEEP);
}
if (messageText.Contains("autisme") || messageText.Contains("autism"))
{
await message.AddReactionAsync(CommonEmoij.AUTISM);
}
*/
}


Expand Down Expand Up @@ -277,14 +273,10 @@ private ServiceProvider ConfigureServices()
return new ServiceCollection()
.AddLogging(opt =>
{
opt.AddConsole();
//opt.AddFilter((category, level) =>
// category == DbLoggerCategory.Database.Command.Name
// && level == LogLevel.Information);
opt.AddConsole(c => c.TimestampFormat = "[yyyy-MM-dd HH:mm:ss] ");
})
.AddSingleton(_config)
.AddSingleton<BotContext>()
//.AddSingleton<DiscordSocketClient>()
.AddSingleton(new DiscordSocketClient(new DiscordSocketConfig
{
AlwaysDownloadUsers = true
Expand All @@ -296,6 +288,7 @@ private ServiceProvider ConfigureServices()
.AddSingleton<PictureService>()
.AddSingleton<AudioService>()
.AddSingleton<QuoteService>()
.AddSingleton<ReminderService>()
.AddSingleton<CommandCountService>()
.AddSingleton<MaintenanceService>()
.AddSingleton<UserService>()
Expand Down
Loading

0 comments on commit 87ea89b

Please sign in to comment.