-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
97 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System.Text; | ||
using Discord.WebSocket; | ||
using DiscordBot.Filters; | ||
using Microsoft.EntityFrameworkCore; | ||
using SharedDependencies.Model; | ||
using SharedDependencies.Services; | ||
|
||
namespace DiscordBot.Services | ||
{ | ||
public class DiscordMessageHandler : IMessageHandler | ||
{ | ||
private readonly DiscordSocketClient _client; | ||
private readonly HouseScoutContext _context; | ||
private readonly DataFilter _dataFilter; | ||
|
||
public DiscordMessageHandler( | ||
DiscordSocketClient client, | ||
HouseScoutContext context, | ||
DataFilter dataFilter | ||
) | ||
{ | ||
_client = client; | ||
_context = context; | ||
_dataFilter = dataFilter; | ||
} | ||
|
||
public async Task HandleMessageAsync() | ||
{ | ||
var users = await _context.Users.AsNoTracking().ToListAsync(); | ||
foreach (var dbUser in users) | ||
{ | ||
var user = await _client.GetUserAsync((ulong)dbUser.UserId); | ||
if (user != null) | ||
{ | ||
var dmChannel = await user.CreateDMChannelAsync(); | ||
var estateData = _dataFilter.SurfacePriceFilter( | ||
dbUser.MinPrice, | ||
dbUser.MaxPrice, | ||
dbUser.MinSurface, | ||
dbUser.MaxSurface | ||
); | ||
|
||
var messages = PrepareMessages(estateData); | ||
|
||
foreach (var message in messages) | ||
{ | ||
await dmChannel.SendMessageAsync(message); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private List<string> PrepareMessages(List<Estate> estateData) | ||
{ | ||
var messages = new List<string>(); | ||
var currentMessage = new StringBuilder(); | ||
|
||
foreach (var estate in estateData) | ||
{ | ||
string estateInfo = $"{estate.Address} - {estate.Price:C} - {estate.Link}\n"; | ||
|
||
if (currentMessage.Length + estateInfo.Length > 2000) | ||
{ | ||
messages.Add(currentMessage.ToString()); | ||
currentMessage.Clear(); | ||
} | ||
|
||
currentMessage.Append(estateInfo); | ||
} | ||
|
||
if (currentMessage.Length > 0) | ||
{ | ||
messages.Add(currentMessage.ToString()); | ||
} | ||
|
||
return messages; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters