Add commands and event handlers
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
package exts
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/dustinpianalto/disgoman"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func pCommand(ctx disgoman.Context, args []string) error {
|
||||
input := strings.Join(args, "")
|
||||
const LENGTH = 1999
|
||||
var mem [LENGTH]byte
|
||||
pointer := 0
|
||||
l := 0
|
||||
for i := 0; i < len(input); i++ {
|
||||
if input[i] == 'L' {
|
||||
if pointer == 0 {
|
||||
pointer = LENGTH - 1
|
||||
} else {
|
||||
pointer--
|
||||
}
|
||||
} else if input[i] == 'R' {
|
||||
if pointer == LENGTH-1 {
|
||||
pointer = 0
|
||||
} else {
|
||||
pointer++
|
||||
}
|
||||
} else if input[i] == '+' {
|
||||
mem[pointer]++
|
||||
} else if input[i] == '-' {
|
||||
mem[pointer]--
|
||||
} else if input[i] == '(' {
|
||||
if mem[pointer] == 0 {
|
||||
i++
|
||||
for l > 0 || input[i] != ')' {
|
||||
if input[i] == '(' {
|
||||
l++
|
||||
}
|
||||
if input[i] == ')' {
|
||||
l--
|
||||
}
|
||||
i++
|
||||
}
|
||||
}
|
||||
} else if input[i] == ')' {
|
||||
if mem[pointer] != 0 {
|
||||
i--
|
||||
for l > 0 || input[i] != '(' {
|
||||
if input[i] == ')' {
|
||||
l++
|
||||
}
|
||||
if input[i] == '(' {
|
||||
l--
|
||||
}
|
||||
i--
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ctx.Send(fmt.Sprintf("Invalid Character: %v", input[i]))
|
||||
return errors.New("invalid character")
|
||||
}
|
||||
}
|
||||
var out []byte
|
||||
for _, i := range mem {
|
||||
if i != 0 {
|
||||
out = append(out, i)
|
||||
}
|
||||
}
|
||||
fmt.Println(out)
|
||||
_, err := ctx.Send(string(out))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package exts
|
||||
|
||||
import (
|
||||
"djpianalto.com/goff/djpianalto.com/goff/utils"
|
||||
"github.com/dustinpianalto/disgoman"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Guild management commands
|
||||
|
||||
func loggingChannel(ctx disgoman.Context, args []string) error {
|
||||
var idString string
|
||||
if len(args) > 0 {
|
||||
idString = args[0]
|
||||
if strings.HasPrefix(idString, "<#") && strings.HasSuffix(idString, ">") {
|
||||
idString = idString[2 : len(idString)-1]
|
||||
}
|
||||
} else {
|
||||
idString = "0"
|
||||
}
|
||||
id, err := strconv.ParseInt(idString, 10, 64)
|
||||
if err != nil {
|
||||
_, _ = ctx.Send("An invalid ID was passed.")
|
||||
return err
|
||||
}
|
||||
if id == 0 {
|
||||
_, err = utils.Database.Exec("UPDATE guilds SET logging_channel=NULL WHERE id=$1;", ctx.Guild.ID)
|
||||
if err != nil {
|
||||
_, _ = ctx.Send("Error Updating Database")
|
||||
}
|
||||
}
|
||||
channel, err := ctx.Session.State.Channel(idString)
|
||||
if err != nil {
|
||||
_, _ = ctx.Send("Can't find that channel.")
|
||||
return err
|
||||
}
|
||||
if channel.GuildID != ctx.Guild.ID {
|
||||
_, _ = ctx.Send("The channel passed is not in this guild.")
|
||||
return err
|
||||
}
|
||||
_, err = utils.Database.Exec("UPDATE guilds SET logging_channel=$1 WHERE id=$2;", id, ctx.Guild.ID)
|
||||
if err != nil {
|
||||
_, _ = ctx.Send("Error Updating Database")
|
||||
return err
|
||||
}
|
||||
_, _ = ctx.Send("Logging Channel Updated.")
|
||||
return nil
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
package exts
|
||||
|
||||
import "github.com/MikeModder/anpan"
|
||||
import (
|
||||
"github.com/dustinpianalto/disgoman"
|
||||
)
|
||||
|
||||
func AddCommandHandlers(h *anpan.CommandHandler) {
|
||||
func AddCommandHandlers(h *disgoman.CommandManager) {
|
||||
// Arguments:
|
||||
// name - command name - string
|
||||
// desc - command description - string
|
||||
@@ -11,8 +13,85 @@ func AddCommandHandlers(h *anpan.CommandHandler) {
|
||||
// perms - permissisions required - anpan.Permission (int)
|
||||
// type - command type, sets where the command is available
|
||||
// run - function to run - func(anpan.Context, []string) / CommandRunFunc
|
||||
h.AddCommand("ping", "Check the bot's ping", false, false, 0, anpan.CommandTypeEverywhere, pingCommand)
|
||||
h.AddCommand("say", "Repeat a message", false, false, 0, anpan.CommandTypeEverywhere, sayCommand)
|
||||
h.AddCommand("user", "Show info about a user", false, false, 0, anpan.CommandTypeEverywhere, userCommand)
|
||||
h.AddCommand("git", "Show my github link", false, false, 0, anpan.CommandTypeEverywhere, gitCommand)
|
||||
h.AddCommand(&disgoman.Command{
|
||||
Name: "ping",
|
||||
Aliases: nil,
|
||||
Description: "Check the bot's ping",
|
||||
OwnerOnly: false,
|
||||
Hidden: false,
|
||||
RequiredPermissions: 0,
|
||||
Invoke: pingCommand,
|
||||
})
|
||||
h.AddCommand(&disgoman.Command{
|
||||
Name: "say",
|
||||
Aliases: nil,
|
||||
Description: "Repeat a message",
|
||||
OwnerOnly: false,
|
||||
Hidden: false,
|
||||
RequiredPermissions: 0,
|
||||
Invoke: sayCommand,
|
||||
})
|
||||
h.AddCommand(&disgoman.Command{
|
||||
Name: "user",
|
||||
Aliases: nil,
|
||||
Description: "Get user info",
|
||||
OwnerOnly: false,
|
||||
Hidden: false,
|
||||
RequiredPermissions: 0,
|
||||
Invoke: userCommand,
|
||||
})
|
||||
h.AddCommand(&disgoman.Command{
|
||||
Name: "git",
|
||||
Aliases: nil,
|
||||
Description: "Show my github link",
|
||||
OwnerOnly: false,
|
||||
Hidden: false,
|
||||
RequiredPermissions: 0,
|
||||
Invoke: gitCommand,
|
||||
})
|
||||
h.AddCommand(&disgoman.Command{
|
||||
Name: "tag",
|
||||
Aliases: nil,
|
||||
Description: "Get a tag",
|
||||
OwnerOnly: false,
|
||||
Hidden: false,
|
||||
RequiredPermissions: 0,
|
||||
Invoke: tagCommand,
|
||||
})
|
||||
h.AddCommand(&disgoman.Command{
|
||||
Name: "addtag",
|
||||
Aliases: nil,
|
||||
Description: "Add a tag",
|
||||
OwnerOnly: false,
|
||||
Hidden: false,
|
||||
RequiredPermissions: 0,
|
||||
Invoke: addTagCommand,
|
||||
})
|
||||
h.AddCommand(&disgoman.Command{
|
||||
Name: "invite",
|
||||
Aliases: nil,
|
||||
Description: "Get the invite link for this bot or others",
|
||||
OwnerOnly: false,
|
||||
Hidden: false,
|
||||
RequiredPermissions: 0,
|
||||
Invoke: inviteCommand,
|
||||
})
|
||||
h.AddCommand(&disgoman.Command{
|
||||
Name: "P",
|
||||
Aliases: nil,
|
||||
Description: "Interpret a P\" program and return the results",
|
||||
OwnerOnly: false,
|
||||
Hidden: false,
|
||||
RequiredPermissions: 0,
|
||||
Invoke: pCommand,
|
||||
})
|
||||
h.AddCommand(&disgoman.Command{
|
||||
Name: "set-logging-channel",
|
||||
Aliases: []string{"slc"},
|
||||
Description: "Set the channel logging messages will be sent to.",
|
||||
OwnerOnly: false,
|
||||
Hidden: false,
|
||||
RequiredPermissions: disgoman.PermissionManageServer,
|
||||
Invoke: loggingChannel,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package exts
|
||||
|
||||
import (
|
||||
"djpianalto.com/goff/djpianalto.com/goff/utils"
|
||||
"fmt"
|
||||
"github.com/dustinpianalto/disgoman"
|
||||
"github.com/kballard/go-shellquote"
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func addTagCommand(ctx disgoman.Context, args []string) error {
|
||||
if len(args) >= 1 {
|
||||
args, err := shellquote.Split(strings.Join(args, " "))
|
||||
if err != nil {
|
||||
ctx.Send(err.Error())
|
||||
return err
|
||||
}
|
||||
queryString := `SELECT tags.id, tags.tag, tags.content from tags
|
||||
WHERE tags.guild_id = $1
|
||||
AND tags.tag = $2;`
|
||||
row := utils.Database.QueryRow(queryString, ctx.Guild.ID, args[0])
|
||||
var dest string
|
||||
if err := row.Scan(&dest); err != nil {
|
||||
tag := args[0]
|
||||
if tag == "" {
|
||||
ctx.Send("That is not a valid tag name")
|
||||
return nil
|
||||
}
|
||||
if len(args) <= 1 {
|
||||
ctx.Send("I got a name but no value.")
|
||||
return nil
|
||||
}
|
||||
value := args[1]
|
||||
if value == "" {
|
||||
ctx.Send("You have to include a content for the tag")
|
||||
return nil
|
||||
}
|
||||
queryString = `INSERT INTO tags (tag, content, creator, guild_id) VALUES ($1, $2, $3, $4);`
|
||||
_, err := utils.Database.Exec(queryString, tag, value, ctx.Message.Author.ID, ctx.Guild.ID)
|
||||
if err != nil {
|
||||
ctx.Send(err.Error())
|
||||
return err
|
||||
}
|
||||
ctx.Send(fmt.Sprintf("Tag %v added successfully.", tag))
|
||||
return nil
|
||||
} else {
|
||||
ctx.Send("That tag already exists.")
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
ctx.Send("You need to tell me what tag you want to add...")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func tagCommand(ctx disgoman.Context, args []string) error {
|
||||
if len(args) >= 1 {
|
||||
tagString := strings.Join(args, " ")
|
||||
queryString := `SELECT tags.id, tags.tag, tags.content from tags
|
||||
WHERE tags.guild_id = $1;`
|
||||
rows, err := utils.Database.Query(queryString, ctx.Guild.ID)
|
||||
if err != nil {
|
||||
ctx.Send(err.Error())
|
||||
return err
|
||||
} else {
|
||||
for rows.Next() {
|
||||
var (
|
||||
id int
|
||||
tag string
|
||||
content string
|
||||
)
|
||||
if err := rows.Scan(&id, &tag, &content); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if tagString == tag {
|
||||
ctx.Send(content)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
ctx.Send(fmt.Sprintf("Tag %v not found", args[0]))
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
ctx.Send("I need a tag to check fetch...")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -3,23 +3,39 @@ package exts
|
||||
import (
|
||||
"djpianalto.com/goff/djpianalto.com/goff/utils"
|
||||
"fmt"
|
||||
"github.com/MikeModder/anpan"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/dustinpianalto/disgoman"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func pingCommand(ctx anpan.Context, _ []string) error {
|
||||
func pingCommand(ctx disgoman.Context, _ []string) error {
|
||||
timeBefore := time.Now()
|
||||
msg, _ := ctx.Reply("Pong!")
|
||||
msg, _ := ctx.Send("Pong!")
|
||||
took := time.Now().Sub(timeBefore)
|
||||
_, err := ctx.Session.ChannelMessageEdit(ctx.Message.ChannelID, msg.ID, fmt.Sprintf("Pong!\nPing Took **%s**", took.String()))
|
||||
return err
|
||||
}
|
||||
|
||||
func gitCommand(ctx anpan.Context, _ []string) error {
|
||||
func inviteCommand(ctx disgoman.Context, args []string) error {
|
||||
var ids []string
|
||||
if len(args) == 0 {
|
||||
ids = []string{ctx.Session.State.User.ID}
|
||||
} else {
|
||||
for _, id := range args {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
}
|
||||
for _, id := range ids {
|
||||
url := fmt.Sprintf("<https://discordapp.com/oauth2/authorize?client_id=%v&scope=bot>", id)
|
||||
ctx.Send(url)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func gitCommand(ctx disgoman.Context, _ []string) error {
|
||||
embed := &discordgo.MessageEmbed{
|
||||
Title: "Hi there, My code is on Github",
|
||||
Color: 0,
|
||||
@@ -29,7 +45,7 @@ func gitCommand(ctx anpan.Context, _ []string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func sayCommand(ctx anpan.Context, args []string) error {
|
||||
func sayCommand(ctx disgoman.Context, args []string) error {
|
||||
resp := strings.Join(args, " ")
|
||||
resp = strings.ReplaceAll(resp, "@everyone", "@\ufff0everyone")
|
||||
resp = strings.ReplaceAll(resp, "@here", "@\ufff0here")
|
||||
@@ -37,7 +53,7 @@ func sayCommand(ctx anpan.Context, args []string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func userCommand(ctx anpan.Context, args []string) error {
|
||||
func userCommand(ctx disgoman.Context, args []string) error {
|
||||
var member *discordgo.Member
|
||||
if len(args) == 0 {
|
||||
member, _ = ctx.Session.GuildMember(ctx.Guild.ID, ctx.Message.Author.ID)
|
||||
|
||||
Reference in New Issue
Block a user