mirror of https://github.com/velour/catbase.git
topic: add plugin
This commit is contained in:
parent
e1ccd553f1
commit
cf5e52c2b6
|
@ -429,3 +429,19 @@ func (d *Discord) Nick(nick string) error {
|
|||
guildID := d.config.Get("discord.guildid", "")
|
||||
return d.client.GuildMemberNickname(guildID, "@me", nick)
|
||||
}
|
||||
|
||||
func (d *Discord) Topic(channelID string) (string, error) {
|
||||
channel, err := d.client.Channel(channelID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return channel.Topic, nil
|
||||
}
|
||||
|
||||
func (d *Discord) SetTopic(channelID, topic string) error {
|
||||
ce := &discordgo.ChannelEdit{
|
||||
Topic: topic,
|
||||
}
|
||||
_, err := d.client.ChannelEditComplex(channelID, ce)
|
||||
return err
|
||||
}
|
||||
|
|
2
main.go
2
main.go
|
@ -4,6 +4,7 @@ package main
|
|||
|
||||
import (
|
||||
"flag"
|
||||
"github.com/velour/catbase/plugins/topic"
|
||||
"io"
|
||||
"math/rand"
|
||||
"os"
|
||||
|
@ -173,6 +174,7 @@ func main() {
|
|||
b.AddPlugin(quotegame.New(b))
|
||||
b.AddPlugin(emojy.New(b))
|
||||
b.AddPlugin(cowboy.New(b))
|
||||
b.AddPlugin(topic.New(b))
|
||||
// catches anything left, will always return true
|
||||
b.AddPlugin(fact.New(b))
|
||||
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
package topic
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/velour/catbase/bot"
|
||||
"github.com/velour/catbase/config"
|
||||
"github.com/velour/catbase/connectors/discord"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
type Topic struct {
|
||||
b bot.Bot
|
||||
c *config.Config
|
||||
}
|
||||
|
||||
func New(b bot.Bot) *Topic {
|
||||
t := &Topic{
|
||||
b: b,
|
||||
c: b.Config(),
|
||||
}
|
||||
t.register()
|
||||
return t
|
||||
}
|
||||
|
||||
func (p *Topic) register() {
|
||||
p.b.RegisterRegexCmd(p, bot.Message, regexp.MustCompile(`(?i)^topic (?P<topic>.+)$`), func(r bot.Request) bool {
|
||||
switch conn := r.Conn.(type) {
|
||||
case *discord.Discord:
|
||||
err := conn.SetTopic(r.Msg.Channel, r.Values["topic"])
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("couldn't set topic")
|
||||
return false
|
||||
}
|
||||
topic, err := conn.Topic(r.Msg.Channel)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("couldn't get topic")
|
||||
return false
|
||||
}
|
||||
p.b.Send(conn, bot.Message, r.Msg.Channel, fmt.Sprintf("Topic: %s", topic))
|
||||
return true
|
||||
|
||||
}
|
||||
return false
|
||||
})
|
||||
p.b.RegisterRegexCmd(p, bot.Message, regexp.MustCompile(`(?i)^topic$`), func(r bot.Request) bool {
|
||||
switch conn := r.Conn.(type) {
|
||||
case *discord.Discord:
|
||||
topic, err := conn.Topic(r.Msg.Channel)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("couldn't get topic")
|
||||
return false
|
||||
}
|
||||
p.b.Send(conn, bot.Message, r.Msg.Channel, fmt.Sprintf("Topic: %s", topic))
|
||||
return true
|
||||
|
||||
}
|
||||
return false
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue