Skip to content

Commit

Permalink
Merge pull request #14 from Xieon/testingprime
Browse files Browse the repository at this point in the history
Testingprime
  • Loading branch information
XieonCrypto authored Apr 12, 2024
2 parents 1a97372 + 84d8f83 commit 009f69b
Show file tree
Hide file tree
Showing 5 changed files with 386 additions and 65 deletions.
42 changes: 40 additions & 2 deletions SysBot.Pokemon.Discord/Commands/General/HelloModule.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using Discord.Commands;
using Discord;
using Discord.Commands;
using System.Linq; // Include the System.Linq namespace.
using System.Threading.Tasks;
using System.IO; //Weed need System.IO to r/w local system files such as "rules.txt"

namespace SysBot.Pokemon.Discord
{
Expand All @@ -14,5 +17,40 @@ public async Task PingAsync()
var msg = string.Format(str, Context.User.Mention);
await ReplyAsync(msg).ConfigureAwait(false);
}
}



[Command("rules")]
[Summary("Get the server rules.")]
public async Task RulesAsync()
{
string folderPath = "variables";
string rulesFilePath = Path.Combine(folderPath, "rules.txt");

// Ensure the folder exists, create it if it doesn't.
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}

if (!File.Exists(rulesFilePath))
{
// If the file doesn't exist, create it with the specified content.
File.WriteAllText(rulesFilePath, "Rules File not Updated Yet");
}

string rulesContent = File.ReadAllText(rulesFilePath);

var embed = new EmbedBuilder
{
Title = "Server Rules",
Description = rulesContent,
Color = new Color(0, 255, 0), // Green success commmand embed -
};

await ReplyAsync(embed: embed.Build());
}


}
}
201 changes: 172 additions & 29 deletions SysBot.Pokemon.Discord/Commands/Management/OwnerModule.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,92 @@
using Discord.Commands;
//Organized into System Libraries and sub Libraries, and Discord
using System;
using System.Linq;
using System.IO; //we need this for local file manipulation
using System.Threading.Tasks;

//I'm going to be using a lot of libraries and subliraries that we not initially used in the original project source code -
//These will be required to use parts of my codeusing Discord.WebSocket; // We need this subclass to be able to attach deleted Pk9 files and restore them.
using Discord.Net; // This is to handle deleted message exceptions
using System.Net; // We need this to catch exceptions for deleted messages
using Discord.Commands;
using Discord;
using System.Collections.Generic;

namespace SysBot.Pokemon.Discord
{
public class OwnerModule : SudoModule
{

private List<ulong> ownerIds; // Declare a field to store the loaded owner IDs


[Command("addSudo")]
[Summary("Adds mentioned user to global sudo")]
[RequireOwner]
[RequireSudo]
// ReSharper disable once UnusedParameter.Global
public async Task SudoUsers([Remainder] string _)
{
ownerIds = LoadOwnerIdsFromFile();

var users = Context.Message.MentionedUsers;
var objects = users.Select(GetReference);
SysCordSettings.Settings.GlobalSudoList.AddIfNew(objects);
await ReplyAsync("Done.").ConfigureAwait(false);
}

private List<ulong> LoadOwnerIdsFromFile()
{
// Define the path to your owners.txt file
string ownersFilePath = Path.Combine("parameters", "owners.txt");

if (File.Exists(ownersFilePath))
{
// Read the file and split it by commas to get a list of owner IDs
string[] ownerIdsStr = File.ReadAllText(ownersFilePath).Split(',');
// Convert the strings to ulong and return the list of owner IDs
List<ulong> ownerIds = ownerIdsStr.Select(str => ulong.Parse(str.Trim())).ToList();
return ownerIds;
}
else
{
// If the file doesn't exist, create it and make it empty
File.WriteAllText(ownersFilePath, string.Empty);

// Return an empty list since there are no owner IDs yet
return new List<ulong>();;
}
}

[Command("removeSudo")]
[Summary("Removes mentioned user from global sudo")]
[RequireOwner]
[RequireSudo]
// ReSharper disable once UnusedParameter.Global
public async Task RemoveSudoUsers([Remainder] string _)
{
var users = Context.Message.MentionedUsers;
var objects = users.Select(GetReference);
SysCordSettings.Settings.GlobalSudoList.RemoveAll(z => objects.Any(o => o.ID == z.ID));
await ReplyAsync("Done.").ConfigureAwait(false);

ulong userId = Context.User.Id;
// Read the contents of owners.txt
string ownersFileContent = File.ReadAllText("parameters/owners.txt");
// Split the content by commas to get an array of owner Discord IDs
string[] ownerIds = ownersFileContent.Split(',');
if (ownerIds.Contains(userId.ToString()))
{
var users = Context.Message.MentionedUsers;
var objects = users.Select(GetReference);
SysCordSettings.Settings.GlobalSudoList.RemoveAll(z => objects.Any(o => o.ID == z.ID));
await ReplyAsync("Done -Owner Removed Sudo from User ").ConfigureAwait(false);
}
else
{
await ReplyAsync("You are a sudo user, but not the owner. You cannot execute this command.");
}
}


// Sudo required
[Command("addChannel")]
[Summary("Adds a channel to the list of channels that are accepting commands.")]
[RequireOwner]
[RequireSudo] // Changed this to require sudo perms instead of owner
// ReSharper disable once UnusedParameter.Global
public async Task AddChannel()
{
Expand All @@ -43,35 +95,86 @@ public async Task AddChannel()
await ReplyAsync("Done.").ConfigureAwait(false);
}

[Command("removeChannel")]
[Summary("Removes a channel from the list of channels that are accepting commands.")]
[RequireOwner]
// ReSharper disable once UnusedParameter.Global
public async Task RemoveChannel()
//This ccommand isn't complete yet - Sudo command only currently
[Command("undelete")]
[Summary("Undeletes a specific message by ID.")]
[RequireSudo]
public async Task UndeleteMessageAsync(ulong messageId)
{
var obj = GetReference(Context.Message.Channel);
SysCordSettings.Settings.ChannelWhitelist.RemoveAll(z => z.ID == obj.ID);
await ReplyAsync("Done.").ConfigureAwait(false);
// Attempt to get the message by its ID using the Channel.
var channel = Context.Channel as ITextChannel;

if (channel != null)
{
var message = await channel.GetMessageAsync(messageId).ConfigureAwait(false);

if (message != null)
{
// Assuming you want to post the undeleted message with author and attachments as files if they exist.
var undeleteMessage = $"{Context.User.Mention} Restoring Deleted message by {message.Author} : {message.Content}";

if (message.Attachments.Any())
{
var attachments = message.Attachments.Select(a => a.Url);
undeleteMessage += $"\nAttachments:\n{string.Join("\n", attachments)}";
}

await channel.SendMessageAsync(undeleteMessage).ConfigureAwait(false);
}

else
{
await ReplyAsync("The specified message does not exist or is not accessible.").ConfigureAwait(false);
}
}

else
{
await ReplyAsync("This command can only be used in a text channel.").ConfigureAwait(false);
}
}

// Sudo + Owners.txt Verification method implemented
[Command("leave")]
[Alias("bye")]
[Summary("Leaves the current server.")]
[RequireOwner]
[RequireSudo]
// ReSharper disable once UnusedParameter.Global
public async Task Leave()
{
await ReplyAsync("Goodbye.").ConfigureAwait(false);
await Context.Guild.LeaveAsync().ConfigureAwait(false);
ulong userId = Context.User.Id;
// Read the contents of owners.txt
string ownersFileContent = File.ReadAllText("parameters/owners.txt");
// Split the content by commas to get an array of owner Discord IDs
string[] ownerIds = ownersFileContent.Split(',');
if (ownerIds.Contains(userId.ToString()))
{
await ReplyAsync($"Goodbye - Exiting the Guild <@{userId}>").ConfigureAwait(false);
await Context.Guild.LeaveAsync().ConfigureAwait(false);
}
else
{
await ReplyAsync("You are a sudo user, but not the owner. You cannot execute this command.");
}
}

[Command("leaveguild")]
// Sudo + Owners.txt Verification method implemented
[Command("leaveguild")]
[Alias("lg")]
[Summary("Leaves guild based on supplied ID.")]
[RequireOwner]
[RequireSudo]
//changed to require sudo and match id in parameters/owner
// ReSharper disable once UnusedParameter.Global
public async Task LeaveGuild(string userInput)
{

ulong userId = Context.User.Id;
// Read the contents of owners.txt
string ownersFileContent = File.ReadAllText("parameters/owners.txt");
// Split the content by commas to get an array of owner Discord IDs
string[] ownerIds = ownersFileContent.Split(',');
if (ownerIds.Contains(userId.ToString()))
{
if (!ulong.TryParse(userInput, out ulong id))
{
await ReplyAsync("Please provide a valid Guild ID.").ConfigureAwait(false);
Expand All @@ -87,30 +190,70 @@ public async Task LeaveGuild(string userInput)

await ReplyAsync($"Leaving {guild}.").ConfigureAwait(false);
await guild.LeaveAsync().ConfigureAwait(false);
}
else
{
await ReplyAsync("You are a sudo user, but not the owner. You cannot execute this command.");
}




}

// Sudo + Owners.txt Verification method implemented
[Command("leaveall")]
[Summary("Leaves all servers the bot is currently in.")]
[RequireOwner]
// ReSharper disable once UnusedParameter.Global
[RequireSudo]
// ReSharper disable once UnusedParameter.Globa
public async Task LeaveAll()
{
await ReplyAsync("Leaving all servers.").ConfigureAwait(false);
foreach (var guild in Context.Client.Guilds)

ulong userId = Context.User.Id;
// Read the contents of owners.txt
string ownersFileContent = File.ReadAllText("parameters/owners.txt");
// Split the content by commas to get an array of owner Discord IDs
string[] ownerIds = ownersFileContent.Split(',');
if (ownerIds.Contains(userId.ToString()))
{
await ReplyAsync("You are the owner. You can execute this command.");
await ReplyAsync("Leaving all servers.").ConfigureAwait(false);
foreach (var guild in Context.Client.Guilds)
{
await guild.LeaveAsync().ConfigureAwait(false);
}
}
else
{
await guild.LeaveAsync().ConfigureAwait(false);
await ReplyAsync("You are a sudo user, but not the owner. You cannot execute this command.");
}


}

// Sudo + Owners.txt Verification method implemented
[Command("sudoku")]
[Alias("kill", "shutdown")]
[Summary("Causes the entire process to end itself!")]
[RequireOwner]
[RequireSudo]
// ReSharper disable once UnusedParameter.Global
public async Task ExitProgram()
{
await Context.Channel.EchoAndReply("Shutting down... goodbye! **Bot services are going offline.**").ConfigureAwait(false);
Environment.Exit(0);
ulong userId = Context.User.Id;
// Read the contents of owners.txt
string ownersFileContent = File.ReadAllText("parameters/owners.txt");
// Split the content by commas to get an array of owner Discord IDs
string[] ownerIds = ownersFileContent.Split(',');
if (ownerIds.Contains(userId.ToString()))
{
await ReplyAsync("You are the owner. You can execute this command.");
await Context.Channel.EchoAndReply("Shutting down... goodbye! **Bot services are going offline.**").ConfigureAwait(false);
Environment.Exit(0);
}
else
{
await ReplyAsync("You are a sudo user, but not the owner. You cannot execute this command.");
}
}

private RemoteControlAccess GetReference(IUser channel) => new()
Expand Down
Loading

0 comments on commit 009f69b

Please sign in to comment.