Skip to content

Commit

Permalink
Merge branch 'release/3.6.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
j4rv committed Apr 18, 2024
2 parents 5fbb40f + 4aa7613 commit ffa5f04
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
10 changes: 3 additions & 7 deletions cmd/jarvbot/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func onMessageCreated(ctx context.Context) func(ds *discordgo.Session, mc *disco
// the command key must be lowercased
var commands = map[string]command{
// public
"!version": simpleTextResponse("v3.6.1"),
"!version": simpleTextResponse("v3.6.2"),
"!source": simpleTextResponse("Source code: https://github.com/j4rv/discord-bot"),
"!genshindailycheckin": answerGenshinDailyCheckIn,
"!genshindailycheckinstop": answerGenshinDailyCheckInStop,
Expand Down Expand Up @@ -263,13 +263,9 @@ func processMessageWithTwitterLinks(ds *discordgo.Session, mc *discordgo.Message
}

messageContent := ogTwitterLinkRegex.ReplaceAllString(mc.Content, "https://fxtwitter.com")
messageContent = fmt.Sprintf("%s:\n%s", mc.Author.Mention(), messageContent)
_, err := ds.ChannelMessageSendComplex(mc.ChannelID, &discordgo.MessageSend{
Content: messageContent,
AllowedMentions: &discordgo.MessageAllowedMentions{},
})
_, err := sendAsUser(ds, mc.Author, mc.ChannelID, messageContent)
if err != nil {
notifyIfErr("processMessageWithTwitterLinks::ds.ChannelMessageSend", err, ds)
notifyIfErr("processMessageWithTwitterLinks::sendAsUser", err, ds)
return
}

Expand Down
30 changes: 30 additions & 0 deletions cmd/jarvbot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,36 @@ func cleanStateMessagesInChannel(ds *discordgo.Session, channel *discordgo.Chann
}
}

func sendAsUserWebhook(ds *discordgo.Session, channelID string) (*discordgo.Webhook, error) {
hooks, err := ds.ChannelWebhooks(channelID)
if err != nil {
return nil, err
}

if len(hooks) == 0 {
return ds.WebhookCreate(channelID, "SendAsUser", ds.State.User.AvatarURL(""))
}

return hooks[0], nil
}

func sendAsUser(ds *discordgo.Session, user *discordgo.User, channelID string, content string) (*discordgo.Message, error) {
if user == nil || ds == nil || channelID == "" || content == "" {
return nil, nil
}

webhook, err := sendAsUserWebhook(ds, channelID)
if err != nil {
return nil, err
}

return ds.WebhookExecute(webhook.ID, webhook.Token, false, &discordgo.WebhookParams{
Content: content,
Username: user.GlobalName,
AvatarURL: user.AvatarURL(""),
})
}

func diff(body, prefix string) string {
lines := strings.Split(body, "\n")
var formattedBody string
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/j4rv/discord-bot
go 1.18

require (
github.com/bwmarrin/discordgo v0.27.1
github.com/bwmarrin/discordgo v0.28.1
github.com/enescakir/emoji v1.0.0
github.com/j4rv/genshinartis v0.0.0-20230911183910-b19c3dc323f3
github.com/j4rv/rollssim v0.0.0-20240404184348-3de18bb2de33
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/bwmarrin/discordgo v0.27.1 h1:ib9AIc/dom1E/fSIulrBwnez0CToJE113ZGt4HoliGY=
github.com/bwmarrin/discordgo v0.27.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
github.com/bwmarrin/discordgo v0.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4=
github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
github.com/enescakir/emoji v1.0.0 h1:W+HsNql8swfCQFtioDGDHCHri8nudlK1n5p2rHCJoog=
github.com/enescakir/emoji v1.0.0/go.mod h1:Bt1EKuLnKDTYpLALApstIkAjdDrS/8IAgTkKp+WKFD0=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
Expand Down

0 comments on commit ffa5f04

Please sign in to comment.