history repeats itself

discordgo -> arikawa
This commit is contained in:
Cynthia Foxwell 2023-07-26 20:27:04 -06:00
parent 7af5462d54
commit 0d6a1398f0
19 changed files with 775 additions and 378 deletions

View file

@ -1,9 +1,11 @@
package events
import "github.com/bwmarrin/discordgo"
import (
"github.com/diamondburned/ningen/v3"
)
func Setup(session *discordgo.Session) {
session.AddHandlerOnce(Ready)
session.AddHandler(MessageCreate)
session.AddHandler(MessageUpdate)
func Setup(session *ningen.State) {
session.PreHandler.AddHandler(Ready)
session.PreHandler.AddHandler(MessageCreate)
session.PreHandler.AddHandler(MessageUpdate)
}

View file

@ -5,65 +5,84 @@ import (
"github.com/Cynosphere/comcord/lib"
"github.com/Cynosphere/comcord/state"
"github.com/bwmarrin/discordgo"
"github.com/diamondburned/arikawa/v3/gateway"
"github.com/diamondburned/arikawa/v3/discord"
)
func MessageCreate(session *discordgo.Session, msg *discordgo.MessageCreate) {
if msg.Author.ID == session.State.User.ID {
return
}
channel, err := session.State.Channel(msg.ChannelID)
func MessageCreate(msg *gateway.MessageCreateEvent) {
client := state.GetClient()
self, err := client.MeStore.Me()
if err != nil {
return
}
isDM := channel.Type == discordgo.ChannelTypeDM || channel.Type == discordgo.ChannelTypeGroupDM
if msg.Author.ID == self.ID {
return
}
channel, err := client.ChannelStore.Channel(msg.ChannelID)
if err != nil {
return
}
isDM := channel.Type == discord.DirectMessage || channel.Type == discord.GroupDM
if state.IsInPrompt() {
state.AddMessageToQueue(msg.Message)
} else {
lines := lib.ProcessMessage(session, msg.Message, lib.MessageOptions{NoColor: state.HasNoColor()})
lines := lib.ProcessMessage(msg.Message, lib.MessageOptions{NoColor: state.HasNoColor()})
for _, line := range lines {
fmt.Print(line)
}
}
if isDM {
state.SetLastDM(msg.ChannelID)
state.SetLastDM(msg.ChannelID.String())
}
}
func MessageUpdate(session *discordgo.Session, msg *discordgo.MessageUpdate) {
if msg.Author == nil {
return
}
if msg.Author.ID == session.State.User.ID {
return
}
if msg.BeforeUpdate != nil && msg.Content == msg.BeforeUpdate.Content {
return
}
channel, err := session.State.Channel(msg.ChannelID)
func MessageUpdate(msg *gateway.MessageUpdateEvent) {
client := state.GetClient()
self, err := client.MeStore.Me()
if err != nil {
return
}
isDM := channel.Type == discordgo.ChannelTypeDM || channel.Type == discordgo.ChannelTypeGroupDM
if msg.Author.ID == self.ID {
return
}
old, err := client.MessageStore.Message(msg.ChannelID, msg.ID)
if err != nil {
return
}
if msg.Content == old.Content {
return
}
// dont process embed updates as messages
if !msg.EditedTimestamp.IsValid() {
return
}
channel, err := client.ChannelStore.Channel(msg.ChannelID)
if err != nil {
return
}
isDM := channel.Type == discord.DirectMessage || channel.Type == discord.GroupDM
if state.IsInPrompt() {
state.AddMessageToQueue(msg.Message)
} else {
lines := lib.ProcessMessage(session, msg.Message, lib.MessageOptions{NoColor: state.HasNoColor()})
lines := lib.ProcessMessage(msg.Message, lib.MessageOptions{NoColor: state.HasNoColor()})
for _, line := range lines {
fmt.Print(line)
}
}
if isDM {
state.SetLastDM(msg.ChannelID)
state.SetLastDM(msg.ChannelID.String())
}
}

View file

@ -6,27 +6,41 @@ import (
"github.com/Cynosphere/comcord/commands"
"github.com/Cynosphere/comcord/state"
"github.com/bwmarrin/discordgo"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/diamondburned/arikawa/v3/gateway"
"github.com/mgutz/ansi"
)
func Ready(session *discordgo.Session, event *discordgo.Ready) {
fmt.Printf("\rLogged in as: %s\n\r", ansi.Color(fmt.Sprintf("%s (%s)", session.State.User.Username, session.State.User.ID), "yellow"))
func Ready(event *gateway.ReadyEvent) {
client := state.GetClient()
self, err := client.Me()
if err != nil {
fmt.Print("\r% Failed to get self: ", err.Error(), "\n\r")
return
}
state.SetNameLength(utf8.RuneCountInString(session.State.User.Username) + 2)
fmt.Printf("\rLogged in as: %s\n\r", ansi.Color(fmt.Sprintf("%s (%s)", self.Username, self.ID), "yellow"))
commands.ListGuildsCommand(session)
state.SetNameLength(utf8.RuneCountInString(self.Username) + 2)
commands.ListGuildsCommand()
defaultGuild := state.GetConfigValue("defaultGuild")
defaultChannel := state.GetConfigValue("defaultChannel")
if defaultGuild != "" {
guild, err := session.State.Guild(defaultGuild)
parsedGuildId, err := discord.ParseSnowflake(defaultGuild)
if err != nil {
fmt.Print("\r% Failed to parse guild ID: ", err.Error(), "\n\r")
return
}
guild, err := client.Guild(discord.GuildID(parsedGuildId))
if err == nil {
if defaultChannel != "" {
state.SetCurrentChannel(defaultChannel)
state.SetLastChannel(defaultGuild, defaultChannel)
}
commands.SwitchGuild(session, guild.Name)
commands.SwitchGuild(guild.Name)
} else {
fmt.Println("\r% This account is not in the defined default guild.")
}