Skip to content

Commit

Permalink
update and quarantine checks, yay!
Browse files Browse the repository at this point in the history
  • Loading branch information
SylveonDeko committed Nov 13, 2023
1 parent c56f1cc commit b53a004
Show file tree
Hide file tree
Showing 7 changed files with 381 additions and 29 deletions.
31 changes: 24 additions & 7 deletions src/Mewdeko/Common/Configs/BotConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public BotConfig()
"Your name is Mewdeko. You are a discord bot. Your profile picture is of the character Hanekawa Tsubasa in Black Hanekawa form. You were created by sylveondeko";
ChatGptMaxTokens = 1000;
ChatGptTemperature = 0.9;
QuarantineNotification = true;
}

[Comment(@"DO NOT CHANGE")]
Expand All @@ -53,17 +54,21 @@ and copy the hex code fo your selected color (marked as #)")]
Allowed values: Simple, Normal, None")]
public ConsoleOutputType ConsoleOutputType { get; set; }

// [Comment(@"For what kind of updates will the bot check.
// Allowed values: Release, Commit, None")]
// public UpdateCheckType CheckForUpdates { get; set; }
[Comment(@"For what kind of updates will the bot check.
Allowed values: Release, Commit, None")]
public UpdateCheckType CheckForUpdates { get; set; }

// [Comment(@"How often will the bot check for updates, in hours")]
// public int CheckUpdateInterval { get; set; }
[Comment(@"How often will the bot check for updates, in hours")]
public int CheckUpdateInterval { get; set; }

[Comment("Set which branch to check for updates")]
public string UpdateBranch { get; set; }

[Comment(@"Do you want any messages sent by users in Bot's DM to be forwarded to the owner(s)?")]
public bool ForwardMessages { get; set; }

[Comment(@"Do you want the message to be forwarded only to the first owner specified in the list of owners (in creds.yml),
[Comment(
@"Do you want the message to be forwarded only to the first owner specified in the list of owners (in creds.yml),
or all owners? (this might cause the bot to lag if there's a lot of owners specified)")]
public bool ForwardToAllOwners { get; set; }

Expand Down Expand Up @@ -127,7 +132,8 @@ This setting can be changed via .rots command.
[Comment("The model to use for chatgpt")]
public string ChatGptModel { get; set; }

[Comment(@"The authorization redirect url for the auth command. This MUST be added to your valid redirect urls in the discord developer portal.")]
[Comment(
@"The authorization redirect url for the auth command. This MUST be added to your valid redirect urls in the discord developer portal.")]
public string RedirectUrl { get; set; }

[Comment("Used to set the error emote used across the bot.")]
Expand All @@ -142,6 +148,10 @@ This setting can be changed via .rots command.
[Comment("Used to set the support server invite on public Mewdeko")]
public string SupportServer { get; set; }

[Comment(
"Notify the owner of the bot when the bot gets quarantined. Only dms first owner if ForwardMessages is enabled.")]
public bool QuarantineNotification { get; set; }


public string Prefixed(string text) => Prefix + text;
}
Expand Down Expand Up @@ -182,4 +192,11 @@ public enum ConsoleOutputType
Normal = 0,
Simple = 1,
None = 2
}

public enum UpdateCheckType
{
Release = 0,
Commit = 1,
None = 2
}
1 change: 1 addition & 0 deletions src/Mewdeko/Mewdeko.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
<PackageReference Include="NonBlocking" Version="2.1.1"/>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.4"/>
<PackageReference Include="NsfwSpy" Version="3.5.0"/>
<PackageReference Include="Octokit" Version="9.0.0"/>
<PackageReference Include="OpenAI" Version="1.7.2"/>
<PackageReference Include="Otp.NET" Version="1.3.0"/>
<PackageReference Include="PokeApiNet" Version="3.0.10"/>
Expand Down
170 changes: 158 additions & 12 deletions src/Mewdeko/Modules/OwnerOnly/OwnerOnly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,25 @@
using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;
using Microsoft.EntityFrameworkCore;
using Octokit;
using Serilog;

namespace Mewdeko.Modules.OwnerOnly;

[OwnerOnly]
public class OwnerOnly(DiscordSocketClient client,
Mewdeko bot,
IBotStrings strings,
InteractiveService serv,
ICoordinator coord,
IEnumerable<IConfigService> settingServices,
DbService db,
IDataCache cache,
CommandService commandService,
IServiceProvider services,
GuildSettingsService guildSettings,
CommandHandler commandHandler)
public class OwnerOnly(
DiscordSocketClient client,
Mewdeko bot,
IBotStrings strings,
InteractiveService serv,
ICoordinator coord,
IEnumerable<IConfigService> settingServices,
DbService db,
IDataCache cache,
CommandService commandService,
IServiceProvider services,
GuildSettingsService guildSettings,
CommandHandler commandHandler)
: MewdekoModuleBase<OwnerOnlyService>
{
public enum SettableUserStatus
Expand All @@ -53,6 +55,150 @@ public async Task ClearUsedTokens()
}
}

[Cmd, Aliases]
public async Task Update()
{
var shell = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "cmd" : "/bin/bash";

var buttons = new ComponentBuilder()
.WithButton("Stable", "stable")
.WithButton("Nightly", "nightly", ButtonStyle.Danger);

var eb = new EmbedBuilder()
.WithOkColor()
.WithDescription("Which version would you like to check updates against?");

var msg = await ctx.Channel.SendMessageAsync(embed: eb.Build(), components: buttons.Build());
var result = await GetButtonInputAsync(ctx.Channel.Id, msg.Id, ctx.User.Id);

if (result is null)
{
await msg.ModifyAsync(x => x.Embed = new EmbedBuilder()
.WithErrorColor()
.WithDescription("Timed out.")
.Build());
return;
}

var branch = result switch
{
"stable" => "main",
"nightly" => "psqldeko",
_ => "main"
};

var process = Process.Start(new ProcessStartInfo
{
FileName = shell,
Arguments = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? "/c \"git rev-parse --abbrev-ref HEAD\""
: "-c \"git rev-parse --abbrev-ref HEAD\"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
});
await process.WaitForExitAsync();
var text = await process.StandardOutput.ReadToEndAsync();
if (text.Replace("\n", "") != branch)
{
if (await PromptUserConfirmAsync(
"Switching branches can cause issues like database incompatibility," +
" or in the case of going from stable to nightly, " +
"major bugs, ***Are you sure you want to continue?***",
ctx.User.Id))
{
await ctx.Channel.SendConfirmAsync("Switching branches and updating, please wait...");
var typing = ctx.Channel.EnterTypingState();
var sw = Stopwatch.StartNew();
var process2 = Process.Start(new ProcessStartInfo
{
FileName = shell,
Arguments = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? $"/c \"git checkout {branch} && git pull\""
: $"-c \"git checkout {branch} && git pull\"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
});
await process2.WaitForExitAsync();
Log.Information("Update Logs: {UpdateLogs}", await process2.StandardOutput.ReadToEndAsync());
sw.Stop();
var eb2 = new EmbedBuilder()
.WithDescription("Update complete.")
.WithOkColor()
.WithFooter("Time taken: " + sw.Elapsed.ToString("g"));
await ctx.Channel.SendMessageAsync(embed: eb2.Build());
typing.Dispose();
}
else
{
await ctx.Channel.SendErrorAsync("Cancelled.");
}
}
else
{
var getCommit = Process.Start(new ProcessStartInfo
{
FileName = shell,
Arguments = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? "/c \"git rev-parse HEAD\""
: "-c \"git rev-parse HEAD\"",
RedirectStandardOutput = true,
CreateNoWindow = true
});
await getCommit.WaitForExitAsync();
var commit = await getCommit.StandardOutput.ReadToEndAsync();
var github = new GitHubClient(new ProductHeaderValue("Mewdeko"));
var repo = await github.Repository.Branch.Get("sylveondeko", "Mewdeko", branch);
if (repo is null)
{
await ctx.Channel.SendErrorAsync(
"Failed to get repo info. Please create an issue on the repo or join the support server.");
return;
}

var commitSha = repo.Commit.Sha;
if (commitSha != commit.Replace("\n", ""))
{
if (await PromptUserConfirmAsync(
"Are you sure you want to update?",
ctx.User.Id))
{
await ctx.Channel.SendConfirmAsync("Updating, please wait...");
var typing = ctx.Channel.EnterTypingState();
var sw = Stopwatch.StartNew();
var process2 = Process.Start(new ProcessStartInfo
{
FileName = shell,
Arguments = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? $"/c git pull"
: $"-c git pull",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
});
await process2.WaitForExitAsync();
Log.Information("Update Logs: {UpdateLogs}", await process2.StandardOutput.ReadToEndAsync());
sw.Stop();
var eb2 = new EmbedBuilder()
.WithDescription("Update complete.")
.WithOkColor()
.WithFooter("Time taken: " + sw.Elapsed.ToString("g"));
await ctx.Channel.SendMessageAsync(embed: eb2.Build());
typing.Dispose();
}
else
{
await ctx.Channel.SendErrorAsync("Cancelled.");
}
}
else
{
await ctx.Channel.SendErrorAsync("Already up to date.");
}
}
}

[Cmd, Aliases]
public async Task Sudo(IGuildUser user, [Remainder] string args)
{
Expand Down
Loading

0 comments on commit b53a004

Please sign in to comment.