comcord/lib/presence.go

122 lines
3.1 KiB
Go
Raw Normal View History

2023-07-17 00:05:04 +00:00
package lib
import (
"context"
2023-07-17 00:05:04 +00:00
"fmt"
"github.com/Cynosphere/comcord/state"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/diamondburned/arikawa/v3/gateway"
2023-07-17 00:05:04 +00:00
)
func UpdatePresence() {
client := state.GetClient()
2023-07-17 00:05:04 +00:00
self, err := client.MeStore.Me()
if err != nil {
return
}
2023-07-17 00:05:04 +00:00
afk := state.IsAFK()
presence := gateway.UpdatePresenceCommand{
2023-07-17 00:05:04 +00:00
Since: 0,
Activities: make([]discord.Activity, 0),
AFK: false,
2023-07-17 00:05:04 +00:00
}
currentGuild := state.GetCurrentGuild()
currentChannel := state.GetCurrentChannel()
parsedGuildId, err := discord.ParseSnowflake(currentGuild)
if err != nil {
return
}
parsedChannelId, err := discord.ParseSnowflake(currentChannel)
if err != nil {
return
}
var activity discord.Activity
2023-07-17 00:05:04 +00:00
startTime := state.GetStartTime()
if self.Bot {
activity = discord.Activity{
Type: discord.GameActivity,
2023-07-17 00:05:04 +00:00
Name: "comcord",
}
if currentGuild != "" && currentChannel != "" {
guild, guildErr := client.GuildStore.Guild(discord.GuildID(parsedGuildId))
channel, channelErr := client.ChannelStore.Channel(discord.ChannelID(parsedChannelId))
2023-07-17 00:05:04 +00:00
if guildErr == nil && channelErr == nil {
2023-08-18 04:57:21 +00:00
activity.Type = discord.CustomActivity
activity.Name = "comcord"
activity.State = fmt.Sprintf("#%s - %s | comcord", channel.Name, guild.Name)
2023-07-17 00:05:04 +00:00
}
}
if afk {
2023-08-18 04:57:21 +00:00
activity.State = activity.State + " [AFK]"
2023-07-17 00:05:04 +00:00
}
} else {
parsedAppId, err := discord.ParseSnowflake("1026163285877325874")
if err != nil {
return
}
activity = discord.Activity{
2023-07-17 00:05:04 +00:00
Type: 0,
AppID: discord.AppID(parsedAppId),
2023-07-17 00:05:04 +00:00
Name: "comcord",
Timestamps: &discord.ActivityTimestamps{
Start: discord.UnixMsTimestamp(startTime.Unix()),
2023-07-17 00:05:04 +00:00
},
/*Buttons: make([]string, 0),
2023-07-17 00:05:04 +00:00
Metadata: ActivityMetadata{
ButtonURLs: make([]string, 0),
},*/
2023-07-17 00:05:04 +00:00
}
//activity.Buttons = append(activity.Buttons, "comcord Repo")
//activity.Metadata.ButtonURLs = append(activity.Metadata.ButtonURLs, "https://gitdab.com/Cynosphere/comcord")
2023-07-17 00:05:04 +00:00
if currentGuild != "" && currentChannel != "" {
guild, guildErr := client.GuildStore.Guild(discord.GuildID(parsedGuildId))
channel, channelErr := client.ChannelStore.Channel(discord.ChannelID(parsedChannelId))
2023-07-17 00:05:04 +00:00
if guildErr == nil && channelErr == nil {
activity.Details = fmt.Sprintf("#%s - %s", channel.Name, guild.Name)
activity.Assets = &discord.ActivityAssets{}
2023-07-17 00:05:04 +00:00
activity.Assets.LargeText = guild.Name
if guild.Icon != "" {
activity.Assets.LargeImage = fmt.Sprintf("mp:icons/%s/%s.png?size=1024", guild.ID, guild.Icon)
2023-07-17 00:05:04 +00:00
}
}
}
if afk {
activity.State = "AFK"
}
}
activity.CreatedAt = discord.UnixTimestamp(startTime.Unix())
2023-07-17 00:05:04 +00:00
presence.Activities = append(presence.Activities, activity)
defaultStatus := state.GetConfigValue("defaultStatus")
if defaultStatus != "" {
presence.Status = discord.Status(defaultStatus)
2023-07-17 00:05:04 +00:00
} else {
if afk {
presence.Status = discord.IdleStatus
2023-07-17 00:05:04 +00:00
} else {
presence.Status = discord.OnlineStatus
2023-07-17 00:05:04 +00:00
}
}
client.Gateway().Send(context.Background(), &presence)
2023-07-17 00:05:04 +00:00
}