-
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.
Branch password compatibility 0.7.4 compatibility
- Loading branch information
Showing
22 changed files
with
358 additions
and
142 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,116 @@ | ||
using System; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using DSharpPlus; | ||
using DSharpPlus.SlashCommands; | ||
using Meds.Shared; | ||
using Meds.Shared.Data; | ||
|
||
namespace Meds.Watchdog.Discord | ||
{ | ||
public class DiscordCmdPlayers : ApplicationCommandModule | ||
{ | ||
private readonly ISubscriber<PlayersResponse> _playersSubscriber; | ||
private readonly IPublisher<PlayersRequest> _playersRequest; | ||
private readonly ISubscriber<PromotePlayerResponse> _promoteSubscriber; | ||
private readonly IPublisher<PromotePlayerRequest> _promoteRequest; | ||
|
||
public DiscordCmdPlayers(ISubscriber<PlayersResponse> playersSubscriber, IPublisher<PlayersRequest> playersRequest, | ||
ISubscriber<PromotePlayerResponse> promoteSubscriber, IPublisher<PromotePlayerRequest> promoteRequest) | ||
{ | ||
_playersSubscriber = playersSubscriber; | ||
_playersRequest = playersRequest; | ||
_promoteSubscriber = promoteSubscriber; | ||
_promoteRequest = promoteRequest; | ||
} | ||
|
||
[SlashCommand("players", "Lists online players")] | ||
[SlashCommandPermissions(Permissions.UseApplicationCommands)] | ||
public async Task PlayersCommand(InteractionContext context) | ||
{ | ||
await context.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWithSource); | ||
try | ||
{ | ||
var response = await _playersSubscriber.AwaitResponse(msg => | ||
{ | ||
var response = new StringBuilder(); | ||
response.Append($"{msg.PlayersLength} Online Players"); | ||
for (var i = 0; i < msg.PlayersLength; i++) | ||
{ | ||
response.Append("\n"); | ||
// ReSharper disable once PossibleInvalidOperationException | ||
var player = msg.Players(i).Value; | ||
if (player.FactionTag != null) | ||
response.Append("[").Append(player.FactionTag).Append("] "); | ||
response.Append(player.Name); | ||
switch (player.Promotion) | ||
{ | ||
case PlayerPromotionLevel.Moderator: | ||
response.Append(" (Moderator)"); | ||
break; | ||
case PlayerPromotionLevel.Admin: | ||
response.Append(" (Admin)"); | ||
break; | ||
case PlayerPromotionLevel.None: | ||
default: | ||
break; | ||
} | ||
} | ||
return response.ToString(); | ||
}, sendRequest: () => | ||
{ | ||
using var token = _playersRequest.Publish(); | ||
PlayersRequest.StartPlayersRequest(token.Builder); | ||
token.Send(PlayersRequest.EndPlayersRequest(token.Builder)); | ||
}); | ||
await context.EditResponseAsync(response); | ||
} | ||
catch (TimeoutException) | ||
{ | ||
await context.EditResponseAsync("Server did not respond to players request. Is it offline?"); | ||
} | ||
} | ||
|
||
public enum DiscordPlayerPromotionLevel | ||
{ | ||
None, | ||
Moderator, | ||
Administrator | ||
} | ||
|
||
[SlashCommand("player-promote", "Promotes or demotes a player")] | ||
[SlashCommandPermissions(Permissions.Administrator)] | ||
public async Task PromotePlayer(InteractionContext context, | ||
[Option("player", "Player steam ID")] [Autocomplete(typeof(DiscordPlayersAutocomplete))] | ||
string steamIdString, | ||
[Option("promotion", "What level to promote / demote the player to")] | ||
DiscordPlayerPromotionLevel promotionLevel) | ||
{ | ||
var steamId = ulong.Parse(steamIdString); | ||
await context.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWithSource); | ||
try | ||
{ | ||
var response = await _promoteSubscriber.AwaitResponse( | ||
msg => | ||
$"{(msg.Successful ? "Promoted" : "Failed to promote")} {msg.Name} ({msg.SteamId}) from {msg.OldPromotion} to {msg.RequestedPromotion}", | ||
sendRequest: () => | ||
{ | ||
using var token = _promoteRequest.Publish(); | ||
token.Send(PromotePlayerRequest.CreatePromotePlayerRequest(token.Builder, steamId, promotionLevel switch | ||
{ | ||
DiscordPlayerPromotionLevel.None => PlayerPromotionLevel.None, | ||
DiscordPlayerPromotionLevel.Moderator => PlayerPromotionLevel.Moderator, | ||
DiscordPlayerPromotionLevel.Administrator => PlayerPromotionLevel.Admin, | ||
_ => throw new ArgumentOutOfRangeException(nameof(promotionLevel), promotionLevel, null) | ||
})); | ||
}); | ||
await context.EditResponseAsync(response); | ||
} | ||
catch (TimeoutException) | ||
{ | ||
await context.EditResponseAsync("Server did not respond to promotion request. Is it offline?"); | ||
} | ||
} | ||
} | ||
} |
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,39 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using DSharpPlus.Entities; | ||
using DSharpPlus.SlashCommands; | ||
using Meds.Shared; | ||
using Meds.Shared.Data; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Meds.Watchdog.Discord | ||
{ | ||
public class DiscordPlayersAutocomplete : IAutocompleteProvider | ||
{ | ||
public Task<IEnumerable<DiscordAutoCompleteChoice>> Provider(AutocompleteContext ctx) | ||
{ | ||
var publisher = ctx.Services.GetRequiredService<IPublisher<PlayersRequest>>(); | ||
var subscriber = ctx.Services.GetRequiredService<ISubscriber<PlayersResponse>>(); | ||
return subscriber.AwaitResponse(response => | ||
{ | ||
var players = new List<DiscordAutoCompleteChoice>(); | ||
for (var i = 0; i < response.PlayersLength; i++) | ||
{ | ||
var player = response.Players(i); | ||
if (!player.HasValue) continue; | ||
var id = player.Value.SteamId; | ||
var name = player.Value.Name; | ||
var level = player.Value.Promotion; | ||
players.Add(new DiscordAutoCompleteChoice($"{name} ({id}) {level}", id.ToString())); | ||
} | ||
return (IEnumerable<DiscordAutoCompleteChoice>) players; | ||
}, TimeSpan.FromSeconds(5), () => | ||
{ | ||
var token = publisher.Publish(); | ||
PlayersRequest.StartPlayersRequest(token.Builder); | ||
token.Send(PlayersRequest.EndPlayersRequest(token.Builder)); | ||
}); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.