Skip to content

Instantly share code, notes, and snippets.

@AlbinoGeek
Last active October 6, 2016 14:55
Show Gist options
  • Save AlbinoGeek/0d7e867bf6ee0017da49050887ba57ae to your computer and use it in GitHub Desktop.
Save AlbinoGeek/0d7e867bf6ee0017da49050887ba57ae to your computer and use it in GitHub Desktop.
Sample Plugin for AsunaBot
namespace DebugPlugin
{
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
// Make sure to add AsunaBot.SDK via NuGet
using AsunaBot.SDK;
using AsunaBot.SDK.Attributes;
// Make sure to add Discord.Net via NuGet
using Discord;
/// <summary>
/// threaded \ref Task (async)
/// </summary>
public class DebugPlugin : IPlugin
{
public string PluginName => "DebugPlugin";
public string PluginMaintainer => "AlbinoStoic";
public string PluginUrl => "https://github.com/AlbinoGeek/AsunaBot/wiki/official-plugins#debugplugin";
public string PluginVersion => System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
public void Init()
{
// Time our execution
runningTimer.Start();
}
private readonly Stopwatch runningTimer = new Stopwatch();
[Command("debug")]
[Description("# shows some information about the arguments")]
private async Task Debug(MessageEventArgs e)
{
var tmp = e.Message.RawText.Split(Utils.Prefix.ToCharArray()[0])[1].Split(new[] { ' ' }, 2);
string command = tmp[0].ToLower();
string arguments = string.Empty;
if (tmp.Length > 1) arguments = tmp[1];
string ret = "Processing '" + command + "'";
if (arguments != string.Empty)
{
ret += " [";
ret = arguments.Split(' ').Aggregate(ret, (current, s) => current + $" \"{s}\",");
ret = ret.Remove(ret.Length - 1, 1) + " ]";
}
await e.Channel.SendMessage($"{ret} {e.User.Mention}");
}
[Command("topic")]
[Description("# shows the current channel's topic message")]
private async Task Topic(MessageEventArgs e)
{
// This command deletes the message that spawned it.
await e.Message.Delete();
// Some channels don't have a topic ...
if (e.Channel.Topic.Length == 0) return;
var tmp = e.Message.RawText.Split(Utils.Prefix.ToCharArray()[0])[1].Split(new[] { ' ' }, 2);
MessageRecipient target = Utils.TargetPicker(e, tmp.Length == 1 ? string.Empty : tmp[1]);
if (target != null)
{
if (target.Id == e.User.Id)
await target.SendMessage($"**{e.Server.Name}#{e.Channel.Name} Topic**:```{e.Channel.Topic}```");
else
await target.SendMessage($"{e.User.Mention} had me send:\n**{e.Server.Name}#{e.Channel.Name} Topic**:```{e.Channel.Topic}```");
}
}
[Command("uptime")]
[Description("# shows how long this bot has been active")]
private async Task Uptime(MessageEventArgs e)
{
// This command deletes the message that spawned it.
await e.Message.Delete();
var tmp = e.Message.RawText.Split(Utils.Prefix.ToCharArray()[0])[1].Split(new[] { ' ' }, 2);
MessageRecipient target = Utils.TargetPicker(e, tmp.Length == 1 ? string.Empty : tmp[1]);
if (target != null)
{
if (target.Id == e.User.Id)
await target.SendMessage($@"**I have lived for**: ```Bot Uptime: {runningTimer.Elapsed}```");
else
await target.SendMessage($@"{e.User.Mention} had me send: **I have lived for**: ```Bot Uptime: {runningTimer.Elapsed}```");
}
}
[Command("who")]
[Command("id")]
[Description("User # shows some information about User")]
private async Task Who(MessageEventArgs e)
{
var tmp = e.Message.RawText.Split(Utils.Prefix.ToCharArray()[0])[1].Split(new[] { ' ' }, 2);
MessageRecipient target = Utils.TargetPicker(e, tmp.Length == 1 ? string.Empty : tmp[1]);
string ret;
if (target?.User != null)
{
// Where a known target was specified, show that target's information
string bot = target.User.IsBot ? "Bot" : "User";
string roles = string.Join(", ", target.User.Roles);
ret = $@"{e.User.Mention}, {tmp[1]} is: ```Id: {target.User.Id} ({bot}) [{target.User.Status}]
Name: {target.User.Name} ({roles})
Joined: {target.User.JoinedAt}``` {target.User.AvatarUrl}";
}
else
{
// Where no target was specified, show caller's information
string bot = e.User.IsBot ? "Bot" : "User";
string roles = string.Join(", ", e.User.Roles);
ret = $@"{e.User.Mention} You are: ```Id: {e.User.Id} ({bot}) [{e.User.Status}]
Name: {e.User.Name} ({roles})
Joined: {e.User.JoinedAt}``` {e.User.AvatarUrl}";
}
await e.Channel.SendMessage(ret);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment