github.com/diamondburned/arikawa@v1.3.14/api/integration_test.go (about)

     1  // +build integration
     2  
     3  package api
     4  
     5  import (
     6  	"fmt"
     7  	"log"
     8  	"os"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/diamondburned/arikawa/discord"
    13  )
    14  
    15  type testConfig struct {
    16  	BotToken  string
    17  	ChannelID discord.ChannelID
    18  }
    19  
    20  func mustConfig(t *testing.T) testConfig {
    21  	var token = os.Getenv("BOT_TOKEN")
    22  	if token == "" {
    23  		t.Fatal("Missing $BOT_TOKEN")
    24  	}
    25  
    26  	var cid = os.Getenv("CHANNEL_ID")
    27  	if cid == "" {
    28  		t.Fatal("Missing $CHANNEL_ID")
    29  	}
    30  
    31  	id, err := discord.ParseSnowflake(cid)
    32  	if err != nil {
    33  		t.Fatal("Invalid $CHANNEL_ID:", err)
    34  	}
    35  
    36  	return testConfig{
    37  		BotToken:  token,
    38  		ChannelID: discord.ChannelID(id),
    39  	}
    40  }
    41  
    42  func TestIntegration(t *testing.T) {
    43  	cfg := mustConfig(t)
    44  
    45  	client := NewClient("Bot " + cfg.BotToken)
    46  
    47  	// Simple GET request
    48  	u, err := client.Me()
    49  	if err != nil {
    50  		t.Fatal("Can't get self:", err)
    51  	}
    52  
    53  	log.Println("API user:", u.Username)
    54  
    55  	// POST with URL param and paginator
    56  	_, err = client.Guilds(100)
    57  	if err != nil {
    58  		t.Fatal("Can't get guilds:", err)
    59  	}
    60  }
    61  
    62  var emojisToSend = [...]string{
    63  	"πŸ₯Ί",
    64  	"❀",
    65  	"πŸ˜‚",
    66  	"πŸ₯°",
    67  	"😊",
    68  	"πŸ”₯",
    69  	"βœ”",
    70  	"πŸ‘",
    71  	"😍",
    72  	"🐻",
    73  	"🀯",
    74  	"πŸ”£",
    75  	"πŸ”",
    76  	"🎌",
    77  	"πŸ‡―πŸ‡΅",
    78  	"πŸŽ₯",
    79  	"πŸ‡ΊπŸ‡Έ",
    80  	"🌎",
    81  }
    82  
    83  func TestReactions(t *testing.T) {
    84  	cfg := mustConfig(t)
    85  
    86  	client := NewClient("Bot " + cfg.BotToken)
    87  
    88  	msg := fmt.Sprintf("This is a message sent at %v.", time.Now())
    89  
    90  	// Send a new message.
    91  	m, err := client.SendMessage(cfg.ChannelID, msg, nil)
    92  	if err != nil {
    93  		t.Fatal("Failed to send message:", err)
    94  	}
    95  
    96  	now := time.Now()
    97  
    98  	for _, emojiString := range emojisToSend {
    99  		if err := client.React(cfg.ChannelID, m.ID, emojiString); err != nil {
   100  			t.Fatal("Failed to send emoji "+emojiString+":", err)
   101  		}
   102  	}
   103  
   104  	msg += fmt.Sprintf(" Total time taken to send all reactions: %v.", time.Now().Sub(now))
   105  
   106  	m, err = client.EditMessage(cfg.ChannelID, m.ID, msg, nil, false)
   107  	if err != nil {
   108  		t.Fatal("Failed to edit message:", err)
   109  	}
   110  }