From d25e7011a62937e62f9d3c2f31b7255f54e137be Mon Sep 17 00:00:00 2001 From: Daniel Perez Date: Wed, 18 Nov 2020 19:59:25 +0100 Subject: [PATCH] Experimental react command --- app/app.go | 1 + commands/commandimpls/react.go | 90 ++++++++++++++++++++++++++++++++++ shortcuts/shortcuts.go | 6 ++- ui/window.go | 15 ++++++ 4 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 commands/commandimpls/react.go diff --git a/app/app.go b/app/app.go index 4f4d3f63..2d29129a 100644 --- a/app/app.go +++ b/app/app.go @@ -126,6 +126,7 @@ func SetupApplicationWithAccount(app *tview.Application, account string) { window.RegisterCommand(serverJoinCmd) window.RegisterCommand(serverLeaveCmd) window.RegisterCommand(serverCreateCmd) + window.RegisterCommand(commandimpls.NewReaction(discord)) window.RegisterCommand(commandimpls.NewServerCommand(serverJoinCmd, serverLeaveCmd, serverCreateCmd)) window.RegisterCommand(commandimpls.NewNickSetCmd(discord, window)) window.RegisterCommand(commandimpls.NewVimCmd(config.Current)) diff --git a/commands/commandimpls/react.go b/commands/commandimpls/react.go new file mode 100644 index 00000000..80834896 --- /dev/null +++ b/commands/commandimpls/react.go @@ -0,0 +1,90 @@ +package commandimpls + +import ( + "fmt" + "io" + + "github.com/Bios-Marcel/discordgo" +) + +const ( + reactionHelp = `[::b]NAME + reaction - show existing message reactions or add new ones + +[::b]SYNOPSIS + [::b]React to a message with react channelID messageID emoji + You can get the first two in chat view by pressing 'w' above a message while in vim mode. + Emojis can be seen by issuing react list + Emoji can either be an unicode emoji or a string. + Show reactions of a message react show channelID messageID + + + +[::b]DESCRIPTION + This is a minor mode for vim. See all the shorcuts with Ctrl K, and edit them inside shortcuts/shortcuts.go + Toggle vim with vim on/off/enable/disable +` +) + +type Reaction struct { + session *discordgo.Session +} + +func NewReaction (s *discordgo.Session) *Reaction{ + return &Reaction{session: s} +} + +// PrintHelp prints a static help page for this command +func (r Reaction) PrintHelp(writer io.Writer) { + fmt.Fprintln(writer, reactionHelp) +} + +func (r Reaction) Execute(writer io.Writer, parameters []string) { + if len(parameters) < 1 { + fmt.Fprintf(writer, "Not enough parameters. Issue man reaction to see help.") + return + } + switch parameters[0] { + case "list": + fmt.Fprintf(writer,"TODO") + case "show": + if len(parameters) < 3 { + fmt.Fprintf(writer, "Not enough arguments provided.") + return + } + message, err := r.session.ChannelMessage(parameters[1], parameters[2]) + if err != nil { + fmt.Fprintf(writer, "There was an error obtaining the message.\n%e",err) + return + } + reactions := message.Reactions + for _, reaction := range reactions { + fmt.Fprintf(writer, "%s\n",reaction.Emoji.Name) + } + return + + default: + if len(parameters) < 3 { + fmt.Fprintf(writer, "Not enough arguments provided for adding reaction.") + return + } + err := r.session.MessageReactionAdd(parameters[0], parameters[1], parameters[2]) + if err != nil { + fmt.Fprintf(writer, "Something wrong ocurred while adding reaction. \n") + return + } + fmt.Fprintf(writer, "Added reaction successfully.") + + } +} + +// Name returns the primary name for this command. This name will also be +// used for listing the command in the commandlist. +func (r Reaction) Name() string { + return "reaction" +} + +// Aliases are a list of aliases for this command. There might be none. +func (r Reaction) Aliases() []string { + return []string{"react", "reaction"} +} diff --git a/shortcuts/shortcuts.go b/shortcuts/shortcuts.go index 02585d70..6491a808 100644 --- a/shortcuts/shortcuts.go +++ b/shortcuts/shortcuts.go @@ -304,7 +304,7 @@ var ( ToggleBareChat = addShortcut("toggle_bare_chat", "Toggle bare chat", globalScope, tcell.NewEventKey(tcell.KeyCtrlB, rune(tcell.KeyCtrlB), tcell.ModCtrl), // FIXME unknown binding - addVimEvent(NullVimEvent,NullVimEvent,NullVimEvent), + addVimEvent(nil,NullVimEvent,NullVimEvent), ) GuildListMarkRead = addShortcut("guild_mark_read", "Mark server as read", @@ -317,6 +317,10 @@ var ( addVimEvent(NullVimEvent,tcell.NewEventKey(tcell.KeyRune, 'm', tcell.ModNone), tcell.NewEventKey(tcell.KeyRune, 'm', tcell.ModNone)), ) + ShowMessageID = addShortcut("show_message_id", "View message id", + chatview, nil, + addVimEvent(NullVimEvent,tcell.NewEventKey(tcell.KeyRune, 'w', tcell.ModNone),NullVimEvent)) + VimInsertMode = addShortcut("vim_insert_mode", "Change to Vim insert mode", globalScope, nil, addVimEvent(tcell.NewEventKey(tcell.KeyRune, 'i', tcell.ModNone),NullVimEvent,NullVimEvent), diff --git a/ui/window.go b/ui/window.go index 6012e035..aaf62c22 100644 --- a/ui/window.go +++ b/ui/window.go @@ -245,6 +245,16 @@ func NewWindow(app *tview.Application, session *discordgo.Session, readyEvent *d window.insertQuoteOfMessage(message) return nil } + if shortcuts.ShowMessageID.Equals(event) { + if !window.commandMode { + window.SetCommandModeEnabled(true) + } + fmt.Fprintf(window.commandView, "Selected channel ID: %s , Current message ID: %s\n", message.ChannelID, message.ID) + clipboard.WriteAll(fmt.Sprintf("%s %s", message.ChannelID, message.ID)) + // window.chatView.formattedMessages + fmt.Fprintf(window.commandView, "ChannelID and MessageID copied to clipboard.\n") + return nil + } if shortcuts.NewDirectMessage.Equals(event) { dmError := window.OpenDirectMessage(message.Author.ID) @@ -2109,6 +2119,11 @@ func (window *Window) handleNotification(message *discordgo.Message, channel *di return beeep.Notify("Cordless - "+notificationLocation, message.ContentWithMentionsReplaced(), "assets/information.png") } +// TODO +func (window *Window) reactToMessage(messageID string, reaction string) { + +} + func (window *Window) askForMessageDeletion(messageID string, usedWithSelection bool) { deleteButtonText := "Delete" window.ShowDialog(tview.Styles.PrimitiveBackgroundColor,