Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discordbot #111

Merged
merged 2 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Content.Server/Administration/ServerApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void IPostInjectInit.PostInject()

// Post
RegisterActorHandler(HttpMethod.Post, "/admin/actions/round/start", ActionRoundStart);
//RegisterActorHandler(HttpMethod.Post, "/admin/actions/ahelp/send", ActionAhelpSend);
RegisterActorHandler(HttpMethod.Post, "/admin/actions/ahelp/send", ActionAhelpSend);
RegisterActorHandler(HttpMethod.Post, "/admin/actions/round/end", ActionRoundEnd);
RegisterActorHandler(HttpMethod.Post, "/admin/actions/round/restartnow", ActionRoundRestartNow);
RegisterActorHandler(HttpMethod.Post, "/admin/actions/kick", ActionKick);
Expand Down Expand Up @@ -398,7 +398,7 @@ await RunOnMainThread(async () =>
});
}

/*private async Task ActionAhelpSend(IStatusHandlerContext context, Actor actor)
private async Task ActionAhelpSend(IStatusHandlerContext context, Actor actor)
{
var body = await ReadJson<DiscordAhelpBody>(context);
if (body == null)
Expand All @@ -408,19 +408,19 @@ await RunOnMainThread(async () =>
await context.RespondErrorAsync(HttpStatusCode.BadRequest);
return;
}
//var _bwoinkSystem = _entitySystemManager.GetEntitySystem<BwoinkSystem>();
var _bwoinkSystem = _entitySystemManager.GetEntitySystem<BwoinkSystem>();
var playerUserId = new NetUserId(body.UserId);
var senderUserId = new NetUserId(actor.Guid);
var message = new SharedBwoinkSystem.BwoinkTextMessage(playerUserId,senderUserId, body.Text);
await RunOnMainThread(async () =>
{
if (_playerManager.TryGetSessionById(playerUserId, out var session))
{
//_bwoinkSystem.DiscordAhelpSendMessage(message, new EntitySessionEventArgs(session));
_bwoinkSystem.DiscordAhelpSendMessage(message, new EntitySessionEventArgs(session));
await RespondOk(context);
}
});
}*/
}

private async Task ShutdownAction(IStatusHandlerContext context, Actor actor)
{
Expand Down
59 changes: 59 additions & 0 deletions Content.Server/Administration/Systems/BwoinkSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -517,5 +517,64 @@ private static string GenerateAHelpMessage(string username, string message, bool
stringbuilder.Append(message);
return stringbuilder.ToString();
}
public void DiscordAhelpSendMessage(BwoinkTextMessage message, EntitySessionEventArgs eventArgs)
{
var senderSession = eventArgs.SenderSession;
// Confirm that this person is actually allowed to send a message here.
var personalChannel = senderSession.UserId == message.UserId;
var senderAdmin = _adminManager.GetAdminData(senderSession);
var senderAHelpAdmin = senderAdmin?.HasFlag(AdminFlags.Adminhelp) ?? false;
var authorized = personalChannel || senderAHelpAdmin;
if (!authorized)
{
// Unauthorized bwoink (log?)
return;
}
var escapedText = FormattedMessage.EscapeText(message.Text);
string bwoinkText;
if (senderAdmin is not null && senderAdmin.Flags == AdminFlags.Adminhelp) // Mentor. Not full admin. That's why it's colored differently.
{
bwoinkText = $"[color=purple]{senderSession.Name}[/color]";
}
else if (senderAdmin is not null && senderAdmin.HasFlag(AdminFlags.Adminhelp))
{
bwoinkText = $"[color=red]{senderSession.Name}[/color]";
}
else
{
bwoinkText = $"{senderSession.Name}";
}
bwoinkText = $"{"(Discord) "}{bwoinkText}: {escapedText}";
// If it's not an admin / admin chooses to keep the sound then play it.
var playSound = !senderAHelpAdmin || message.PlaySound;
var msg = new BwoinkTextMessage(message.UserId, senderSession.UserId, bwoinkText, playSound: playSound);
LogBwoink(msg);
var admins = GetTargetAdmins();
// Notify all admins
foreach (var channel in admins)
{
RaiseNetworkEvent(msg, channel);
}
var sendsWebhook = _webhookUrl != string.Empty;
if (sendsWebhook)
{
if (!_messageQueues.ContainsKey(msg.UserId))
_messageQueues[msg.UserId] = new Queue<string>();
var str = message.Text;
var unameLength = senderSession.Name.Length;
if (unameLength + str.Length + _maxAdditionalChars > DescriptionMax)
{
str = str[..(DescriptionMax - _maxAdditionalChars - unameLength)];
}
var nonAfkAdmins = GetNonAfkAdmins();
_messageQueues[msg.UserId].Enqueue(GenerateAHelpMessage(senderSession.Name, str, !personalChannel, _gameTicker.RoundDuration().ToString("hh\\:mm\\:ss"), _gameTicker.RunLevel, playedSound: playSound, noReceivers: nonAfkAdmins.Count == 0));
}
if (admins.Count != 0 || sendsWebhook)
return;
// No admin online, let the player know
var systemText = Loc.GetString("bwoink-system-starmute-message-no-other-users");
var starMuteMsg = new BwoinkTextMessage(message.UserId, SystemUserId, systemText);
RaiseNetworkEvent(starMuteMsg, senderSession.Channel);
}
}
}
Loading