Files
SethDiscordBot/DiscordBotWebUI/Components/Pages/Settings.razor

119 lines
3.7 KiB
Plaintext

@page "/settings"
@using DiscordBotCore
@inject NotificationService NotificationService
<RadzenPanel>
<HeaderTemplate>
<RadzenText>Discord Bot requires a Bot Token, a prefix for normal commands and the server Ids on the servers it will be in</RadzenText>
</HeaderTemplate>
<ChildContent>
<RadzenRow>
<RadzenColumn>
<RadzenText>Discord Bot Token</RadzenText>
</RadzenColumn>
<RadzenColumn>
<RadzenTextBox Placeholder="Token..." Value="@_Token" ValueChanged="TokenValueChanged"></RadzenTextBox>
</RadzenColumn>
</RadzenRow>
<RadzenRow>
<RadzenColumn>
<RadzenText>Bot prefix</RadzenText>
</RadzenColumn>
<RadzenColumn>
<RadzenTextBox Placeholder="Bot prefix ..." MaxLength="1" Value="@_Prefix" ValueChanged="PrefixValueChanged"></RadzenTextBox>
</RadzenColumn>
</RadzenRow>
<RadzenRow>
<RadzenColumn>
<RadzenText>Bot Server Ids:</RadzenText>
</RadzenColumn>
<RadzenColumn>
<RadzenTextArea Placeholder="One per line ..." Value="@_ServerIds" ValueChanged="ServerIDsValueChanged"></RadzenTextArea>
</RadzenColumn>
</RadzenRow>
</ChildContent>
<FooterTemplate>
<RadzenButton Text="Save" Click="SaveChanges"></RadzenButton>
</FooterTemplate>
</RadzenPanel>
@code {
private string _Token = string.Empty;
private string _Prefix = string.Empty;
private string _ServerIds = string.Empty;
[Parameter]
public Action? OnSaveChanged { get; set; }
protected override void OnInitialized()
{
base.OnInitialized();
if(Application.CurrentApplication.ApplicationEnvironmentVariables.TryGetValue("token", out var token))
{
_Token = token as string;
}
if(Application.CurrentApplication.ApplicationEnvironmentVariables.TryGetValue("prefix", out var prefix))
{
_Prefix = prefix as string;
}
if(Application.CurrentApplication.ApplicationEnvironmentVariables.TryGetValue("ServerID", out var serverIds))
{
if (serverIds is List<object> listServerIds)
{
foreach(var item in listServerIds)
{
_ServerIds += $"{item}\n";
}
_ServerIds.TrimEnd();
}
}
StateHasChanged();
}
private async Task SaveChanges()
{
Application.CurrentApplication.ApplicationEnvironmentVariables.Set("token", _Token);
Application.CurrentApplication.ApplicationEnvironmentVariables.Set("prefix", _Prefix);
List<ulong> serverIds = new List<ulong>();
string[] values = _ServerIds.Split('\n');
foreach(var value in values)
{
string clearValue = value.TrimEnd().Replace("\r", "");
if(ulong.TryParse(clearValue, out ulong actualValue))
{
serverIds.Add(actualValue);
}
}
Application.CurrentApplication.ApplicationEnvironmentVariables.Set("ServerID", serverIds);
await Application.CurrentApplication.ApplicationEnvironmentVariables.SaveToFile();
NotificationService.Notify(NotificationSeverity.Success, "Configuration", "Configuration has been saved !", 4000);
OnSaveChanged?.Invoke();
}
private void ServerIDsValueChanged(string obj)
{
_ServerIds = obj;
}
private void PrefixValueChanged(string obj)
{
_Prefix = obj;
}
private void TokenValueChanged(string obj)
{
_Token = obj;
}
}