11using System . Globalization ;
22using Borealis . Core . Contracts ;
33using Borealis . Core . Models ;
4+ using Borealis . Core . Options ;
45using Discord ;
56using Microsoft . EntityFrameworkCore ;
7+ using Microsoft . Extensions . Options ;
68
79namespace Borealis . Core . Services ;
810
911public class DiscordBotService : IDiscordBotService {
1012 private readonly BorealisContext _borealisContext ;
1113 private readonly IDiscordClient _discordClient ;
14+ private readonly IOptions < BorealisOptions > _borealisOptions ;
1215 private readonly ILogger < DiscordBotService > _logger ;
1316
14- public DiscordBotService ( BorealisContext borealisContext , IDiscordClient discordClient , ILogger < DiscordBotService > logger ) {
17+ public DiscordBotService (
18+ BorealisContext borealisContext ,
19+ IDiscordClient discordClient ,
20+ IOptions < BorealisOptions > borealisOptions ,
21+ ILogger < DiscordBotService > logger ) {
1522 _borealisContext = borealisContext ;
1623 _discordClient = discordClient ;
24+ _borealisOptions = borealisOptions ;
1725 _logger = logger ;
1826 }
1927
@@ -30,7 +38,23 @@ public DiscordBotService(BorealisContext borealisContext, IDiscordClient discord
3038 return settings ;
3139 }
3240
33- public async Task SendMessageAsync ( string ? channelId , string message , CancellationToken cancellationToken ) {
41+ private async Task SendPlayerMessageAsync ( string ? channelId , string message , Player player ) {
42+ var options = _borealisOptions . Value ;
43+
44+ var builder = new ComponentBuilder ( ) ;
45+
46+ // TODO Add containers and stuff when supported
47+
48+ if ( ! string . IsNullOrWhiteSpace ( options . ApplicationUrl ) ) {
49+ builder . AddRow ( new ActionRowBuilder ( )
50+ . WithButton ( "View player" , style : ButtonStyle . Link , url : $ "{ options . ApplicationUrl } players/{ player . Id } ")
51+ ) ;
52+ }
53+
54+ await SendMessageAsync ( channelId , message , builder . Build ( ) ) ;
55+ }
56+
57+ public async Task SendMessageAsync ( string ? channelId , string message , MessageComponent ? messageComponent = null ) {
3458 if ( ! ulong . TryParse ( channelId , out var parseChannelId ) ) {
3559 _logger . LogWarning ( "Invalid channel ID: {ChannelId}" , channelId ) ;
3660 return ;
@@ -41,7 +65,7 @@ public async Task SendMessageAsync(string? channelId, string message, Cancellati
4165 throw new InvalidOperationException ( $ "Channel { parseChannelId } is not a valid message channel.") ;
4266 }
4367
44- await channel . SendMessageAsync ( message ) ;
68+ await channel . SendMessageAsync ( message , components : messageComponent ) ;
4569 }
4670
4771 public async Task SendGiftCodeAddedMessageAsync ( GiftCode giftCode , CancellationToken cancellationToken ) {
@@ -50,8 +74,18 @@ public async Task SendGiftCodeAddedMessageAsync(GiftCode giftCode, CancellationT
5074 return ;
5175 }
5276
77+ var options = _borealisOptions . Value ;
78+
79+ var builder = new ComponentBuilder ( ) ;
80+
81+ if ( ! string . IsNullOrWhiteSpace ( options . ApplicationUrl ) ) {
82+ builder . AddRow ( new ActionRowBuilder ( )
83+ . WithButton ( "View gift code" , style : ButtonStyle . Link , url : $ "{ options . ApplicationUrl } gift-codes/{ giftCode . Id } ")
84+ ) ;
85+ }
86+
5387 var message = $ "New gift code found: { giftCode . Code } ";
54- await SendMessageAsync ( settings . GiftCodeChannelId , message , cancellationToken ) ;
88+ await SendMessageAsync ( settings . GiftCodeChannelId , message , builder . Build ( ) ) ;
5589 }
5690
5791 public async Task SendPlayerChangedNameMessageAsync ( Player player , string newName , string oldName , CancellationToken cancellationToken ) {
@@ -64,8 +98,18 @@ public async Task SendPlayerChangedNameMessageAsync(Player player, string newNam
6498 return ;
6599 }
66100
67- var message = $ "Player { oldName } changed their name to { newName } .";
68- await SendMessageAsync ( settings . PlayerRenameChannelId , message , cancellationToken ) ;
101+ var previousNames = player . PreviousNames
102+ . Select ( x => x . Name )
103+ . Except ( [ newName , oldName ] )
104+ . Distinct ( )
105+ . ToList ( ) ;
106+
107+ var message = $ "Player { oldName } (#{ player . State } ) changed their name to { newName } .";
108+ if ( previousNames . Count > 0 ) {
109+ message += $ " Previous names: { string . Join ( ", " , previousNames ) } .";
110+ }
111+
112+ await SendPlayerMessageAsync ( settings . PlayerRenameChannelId , message , player ) ;
69113 }
70114
71115 public async Task SendPlayerChangedFurnaceLevelMessageAsync ( Player player , string furnaceLevel , CancellationToken cancellationToken ) {
@@ -78,8 +122,8 @@ public async Task SendPlayerChangedFurnaceLevelMessageAsync(Player player, strin
78122 return ;
79123 }
80124
81- var message = $ "Player { player . Name } increased their furnace level to { furnaceLevel } .";
82- await SendMessageAsync ( settings . PlayerFurnaceLevelChannelId , message , cancellationToken ) ;
125+ var message = $ "Player { player . Name } (# { player . State } ) increased their furnace level to { furnaceLevel } .";
126+ await SendPlayerMessageAsync ( settings . PlayerFurnaceLevelChannelId , message , player ) ;
83127 }
84128
85129 public async Task SendPlayerChangedStateMessageAsync ( Player player , int newState , int oldState , CancellationToken cancellationToken ) {
@@ -93,7 +137,7 @@ public async Task SendPlayerChangedStateMessageAsync(Player player, int newState
93137 }
94138
95139 var message = $ "Player { player . Name } moved state from { oldState } to { newState } .";
96- await SendMessageAsync ( settings . PlayerMovedStateChannelId , message , cancellationToken ) ;
140+ await SendPlayerMessageAsync ( settings . PlayerMovedStateChannelId , message , player ) ;
97141 }
98142
99143 public async Task < IReadOnlyCollection < DiscordGuild > > GetGuildsAsync ( CancellationToken cancellationToken ) {
@@ -104,6 +148,7 @@ public async Task<IReadOnlyCollection<DiscordGuild>> GetGuildsAsync(Cancellation
104148 GuildId = guild . Id . ToString ( CultureInfo . InvariantCulture ) ,
105149 Name = guild . Name
106150 } )
151+ . OrderBy ( guild => guild . Name , StringComparer . OrdinalIgnoreCase )
107152 ] ;
108153 }
109154
@@ -118,6 +163,7 @@ public async Task<IReadOnlyCollection<DiscordChannel>> GetChannelsAsync(string g
118163 ChannelId = channel . Id . ToString ( CultureInfo . InvariantCulture ) ,
119164 Name = channel . Name
120165 } )
166+ . OrderBy ( guild => guild . Name , StringComparer . OrdinalIgnoreCase )
121167 ] ;
122168 }
123169}
0 commit comments