Add command that allows sending of files to the current channel
MODIFY
app/app.go
app/app.go
@@ -97,6 +97,7 @@ func Run() {
}
window.RegisterCommand(commandimpls.NewStatusCommand(discord))
+ window.RegisterCommand(commandimpls.NewFileSendCommand(discord, window))
window.RegisterCommand(commandimpls.NewAccount(runNext, window))
window.RegisterCommand(commandimpls.NewHelpCommand(window))
window.RegisterCommand(commandimpls.NewManualCommand())

ADD
commands/commandimpls/file-send.go
+package commandimpls
+
+import (
+ "bytes"
+ "fmt"
+ "io"
+ "io/ioutil"
+ "os/user"
+ "path"
+ "path/filepath"
+ "strings"
+
+ "github.com/Bios-Marcel/cordless/ui"
+ "github.com/Bios-Marcel/discordgo"
+)
+
+const fileSendDocumentation = `[orange][::u]# file-send command[white]
+
+The file-send command allows you to send multiple files to your current channel.
+
+Usage:
+ file-send <FILE_PATH>...
+
+Examples:
+ file-send ~/file.txt
+ file-send ~/file1.txt ~/file2.txt
+ file-send "~/file one.txt" ~/file2.txt
+`
+
+// FileSend represents the command used to send multiple files to a channel.
+type FileSend struct {
+ discord *discordgo.Session
+ window *ui.Window
+}
+
+// NewFileSendCommand creates a ready to use FileSend instance.
+func NewFileSendCommand(discord *discordgo.Session, window *ui.Window) *FileSend {
+ return &FileSend{
+ discord: discord,
+ window: window,
+ }
+}
+
+// Execute runs the command piping its output into the supplied writer.
+func (cmd *FileSend) Execute(writer io.Writer, parameters []string) {
+ channel := cmd.window.GetSelectedChannel()
+ if channel == nil {
+ fmt.Fprintln(writer, "[red]In order to sue this command, you have to be in a channel.")
+ return
+ }
+
+ if len(parameters) == 0 {
+ cmd.PrintHelp(writer)
+ return
+ }
+
+ for _, parameter := range parameters {
+ var resolvedPath string
+ if strings.HasPrefix(parameter, "~") {
+ currentUser, userResolveError := user.Current()
+ if userResolveError != nil {
+ fmt.Fprintf(writer, "[red]Error resolving path:\n\t[red]%s\n", userResolveError.Error())
+ continue
+ }
+
+ resolvedPath = filepath.Join(currentUser.HomeDir, strings.TrimPrefix(parameter, "~"))
+ } else {
+ resolvedPath = parameter
+ }
+
+ resolvedPath, resolveError := filepath.EvalSymlinks(resolvedPath)
+ if resolveError != nil {
+ fmt.Fprintf(writer, "[red]Error resolving path:\n\t[red]%s\n", resolveError.Error())
+ continue
+ }
+
+ isAbs := filepath.IsAbs(resolvedPath)
+ if !isAbs {
+ fmt.Fprintln(writer, "[red]Error reading file:\n\tthe path is not absolute")
+ continue
+ }
+
+ data, readError := ioutil.ReadFile(resolvedPath)
+ if readError != nil {
+ fmt.Fprintf(writer, "[red]Error reading file:\n\t[red]%s\n", readError.Error())
+ continue
+ }
+
+ dataChannel := bytes.NewReader(data)
+ _, sendError := cmd.discord.ChannelFileSend(channel.ID, path.Base(resolvedPath), dataChannel)
+ if sendError != nil {
+ fmt.Fprintf(writer, "[red]Error sending file:\n\t[red]%s\n", sendError.Error())
+ }
+ }
+}
+
+// Name is the string that can be used to call this command.
+func (cmd *FileSend) Name() string {
+ return "file-send"
+}
+
+// PrintHelp prints the help for the FileSend command.
+func (cmd *FileSend) PrintHelp(writer io.Writer) {
+ fmt.Fprint(writer, fileSendDocumentation)
+}

MODIFY
ui/window.go
ui/window.go
@@ -1775,6 +1775,11 @@ func (window *Window) GetSelectedGuild() *discordgo.UserGuild {
return window.selectedGuild
}
+// GetSelectedChannel returns a reference to the currently selected Channel.
+func (window *Window) GetSelectedChannel() *discordgo.Channel {
+ return window.selectedChannel
+}
+
// PromptSecretInput shows an input dialog that masks the user input. The
// returned value will either be empty or what the user has entered.
func (window *Window) PromptSecretInput(title, message string) string {

Add command that allows sending of files to the current channel