Skip to content

Commit

Permalink
Merge branch 'main' of github.com:BattleRush/ETH-DINFK-Bot
Browse files Browse the repository at this point in the history
  • Loading branch information
BattleRush committed Oct 8, 2024
2 parents e412a2a + 343d45f commit e40279c
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 14 deletions.
6 changes: 3 additions & 3 deletions ETHDINFKBot/CronJobs/Jobs/Food2050TickerJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,13 @@ public async void GetTickerData()

if(message.Length > 1800)
{
await channel.SendMessageAsync(message.Substring(0, 2000));
//await channel.SendMessageAsync(message.Substring(0, 2000));
message = "";
}
}

if (!string.IsNullOrEmpty(message))
await channel.SendMessageAsync(message);
//if (!string.IsNullOrEmpty(message))
//await channel.SendMessageAsync(message);

if (!string.IsNullOrEmpty(errorLog))
{
Expand Down
1 change: 1 addition & 0 deletions ETHDINFKBot/ETHDINFKBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<PackageReference Include="SkiaSharp" Version="2.88.6" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="2.88.6" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.88.6" />
<PackageReference Include="System.IO.Packaging" Version="8.0.0" />
<PackageReference Include="TheMulti0.DuckSharp" Version="1.0.2" />
<PackageReference Include="TimeZoneConverter" Version="6.1.0" />
<PackageReference Include="websocketsharp.core" Version="1.0.0" />
Expand Down
101 changes: 90 additions & 11 deletions ETHDINFKBot/Modules/AdminModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
using System.Runtime.InteropServices;
using Discord.Net;
using System.ComponentModel.DataAnnotations;
using System.IO.Packaging;


namespace ETHDINFKBot.Modules
{
Expand Down Expand Up @@ -68,26 +70,103 @@ public class EmoteInfo
public class AdminModule : ModuleBase<SocketCommandContext>
{

static void CreateZipFromStreams(Dictionary<string, Stream> fileStreams, string zipFilePath)
{
// make sure the file is not there
if (File.Exists(zipFilePath))
File.Delete(zipFilePath);

// Create a ZIP package
using (Package zip = Package.Open(zipFilePath, FileMode.Create))
{
foreach (var entry in fileStreams)
{
string fileName = entry.Key;
Stream fileStream = entry.Value;

// Create a unique URI for the file part in the ZIP package
Uri fileUri = PackUriHelper.CreatePartUri(new Uri(fileName, UriKind.Relative));

// Check if the part already exists in the package
if (zip.PartExists(fileUri))
{
// Optionally, generate a new file name or skip the duplicate
int counter = 1;
string fileExtension = Path.GetExtension(fileName);
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);

// Generate a unique file name by appending a counter to the original name
do
{
fileName = $"{fileNameWithoutExtension}_{counter}{fileExtension}";
fileUri = PackUriHelper.CreatePartUri(new Uri(fileName, UriKind.Relative));
counter++;
} while (zip.PartExists(fileUri));
}

// Create the part for the file
PackagePart packagePart = zip.CreatePart(fileUri, System.Net.Mime.MediaTypeNames.Application.Octet);

// Copy the stream content to the part
using (Stream partStream = packagePart.GetStream())
{
fileStream.CopyTo(partStream);
}
}
}
}
[Command("emotesync")]
[RequireUserPermission(GuildPermission.ManageChannels)]
public async Task SyncEmotes()
{
ulong serverId = Context.Guild.Id;
try
{
ulong serverId = Context.Guild.Id;

// find all emotes with this server id and clear it
int returnedCleared = DatabaseManager.EmoteDatabaseManager.ClearServerEmotes(serverId);

// get all emotes from the server
var emotes = Context.Guild.Emotes;

List<ulong> emoteIds = new List<ulong>();

// list of emote streams
Dictionary<string, Stream> emoteStreams = new Dictionary<string, Stream>();

await Context.Channel.SendMessageAsync($"Found {emotes.Count} emotes downloading...", false);

using (var webClient = new WebClient())
{
foreach (var emote in emotes)
{
byte[] bytes = webClient.DownloadData(emote.Url);
Stream stream = new MemoryStream(bytes);
emoteStreams.Add(emote.Name + (emote.Animated ? ".gif" : ".png"), stream);
emoteIds.Add(emote.Id);
}
}

// find all emotes with this server id and clear it
int returnedCleared = DatabaseManager.EmoteDatabaseManager.ClearServerEmotes(serverId);
// create zip with all emotes from emoteStreams
string zipFilePath = "output.zip";

// get all emotes from the server
var emotes = Context.Guild.Emotes;
CreateZipFromStreams(emoteStreams, zipFilePath);

List<ulong> emoteIds = new List<ulong>();
// upload zip to discord
await Context.Channel.SendFileAsync(zipFilePath, "Emotes.zip");

foreach (var emote in emotes)
emoteIds.Add(emote.Id);
// delete zip file
File.Delete(zipFilePath);

int setEmotes = DatabaseManager.EmoteDatabaseManager.SetServerEmotes(emoteIds, serverId);

await Context.Channel.SendMessageAsync($"Cleared {returnedCleared} and set {setEmotes} emotes", false);
int setEmotes = DatabaseManager.EmoteDatabaseManager.SetServerEmotes(emoteIds, serverId);

await Context.Channel.SendMessageAsync($"Cleared {returnedCleared} and set {setEmotes} emotes", false);
}
catch (Exception ex)
{
await Context.Channel.SendMessageAsync(ex.ToString(), false);
}
}


Expand Down Expand Up @@ -1747,7 +1826,7 @@ public async Task Insert2050Mensa(string link)

if (!anyKey)
{
await Context.Channel.SendMessageAsync($"No similar restaurant found for {dbRestaurantName}", false);
await Context.Channel.SendMessageAsync($"No similar restaurant found for {dbRestaurantName}", false);
}
}
else
Expand Down
1 change: 1 addition & 0 deletions ETHDINFKBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ public async Task MainAsync(string token)
#if DEBUG
www = @"C:\Temp\wss";
#endif
// battlerush.dev cdhs22.battlerush.dev cdn.battlerush.dev place.battlerush.dev ws.battlerush.dev www.battlerush.dev
// Create and prepare a new SSL server context
// TO REGENERATE "sudo openssl pkcs12 -export -out battlerush.dev.pfx -inkey privkey.pem -in fullchain.pem"
//var context = new SslContext(SslProtocols.Tls13, new X509Certificate2(Path.Combine(Configuration["CertFilePath"], "battlerush.dev.pfx"), ""));
Expand Down

0 comments on commit e40279c

Please sign in to comment.