Skip to content
This repository was archived by the owner on May 31, 2024. It is now read-only.

Relationships

LennyPhoenix edited this page Jun 2, 2021 · 21 revisions

This module (and its corresponding manager) helps you access the relationships your players have made on Discord, allowing you to:

  • Access a user's relationships.
  • Filter those relationships based on a given criteria.
  • Build a user's friend list.

Contents

Status (Enum)

Discord.Status

Description

Enum for the different user statuses.

Items

  • 0: OFFLINE
  • 1: ONLINE
  • 2: IDLE
  • 3: DO_NOT_DISTURB

Relationship Type (Enum)

Discord.RelationshipType

Description

Enum for the different types of relationships.

Items

  • 0: NONE
  • 1: FRIEND
  • 2: BLOCKED
  • 3: PENDING_INCOMING
  • 4: PENDING_OUTGOING
  • 5: IMPLICIT

Presence

Discord.Presence

Extends Resource.

Description

Contains information about a user's presence.

Properties

Status

status: Discord.Status

The user's current online status.

Default: Discord.Status.OFFLINE

Activity

activity: Discord.Activity

The user's current activity.

Default: new Discord.Activity (Will appear as [empty] in the editor due to current limitations.)

Relationship

Discord.Relationship

Extends Resource.

Description

Contains information about a relationship with a user.

Properties

Type

type: Discord.RelationshipType

What kind of relationship it is.

Default: Discord.RelationshipType.NONE

User

user: Discord.User

The user the relationship is for.

Default: new Discord.User (Will appear as [empty] in the editor due to current limitations.)

Presence

presence: Discord.Presence

The user's current presence.

Default: new Discord.Presence (Will appear as [empty] in the editor due to current limitations.)

Relationship Manager

Discord.RelationshipManager

Extends Object.

Description

Contains methods and events related to relationships.

Methods

Filter

filter(filter_target: Object, filter_method: String)

Sets the filter for any future relationship commands. Filter should return a boolean. Because signals cannot return a value, this method does not have a corresponding signal callback.

Should only be called after the refresh signal is released. Note that because of caching the filter cannot be set multiple times as far as I know, so you will want to set this to the broadest filter you are going to do then do any other filtering yourself.

Arguments

  • filter_target: Object - The filter target.

    filter_method: String - The filter method's name.

    filter(relationship: Discord.Relationship) -> bool

Example

var discord: Discord.Core
var relationships: Discord.RelationshipManager


func _ready() -> void:
	# ...
	yield(relationships, "refresh") # Wait for refresh
	relationships.filter(self, "_filter")


func _filter(relationship: Discord.Relationship) -> bool:
	return relationship.type == Discord.RelationshipType.FRIEND  # Only return `true` if they are a friend.

Get

get(user_id: int, [callback_target: Object, callback_method: String]) -> void

Get the relationship between the current user and a given user by id. Has a corresponding signal get_callback that can be used instead of the optional target & method args.

Should only be called after the refresh signal is released.

Arguments

  • user_id: int - The ID of the user to fetch.

Optional Args

  • callback_target: Object - The callback target.

    callback_method: String - The callback method's name.

    get_callback(result: Discord.Result, relationship: Discord.Relationship)

Example

var discord: Discord.Core
var relationships: Discord.RelationshipManager


func _ready() -> void:
	# ...
	yield(relationships, "refresh")
	relationships.get(
		425340416531890178,
		self, "_get_callback"
	)


func _get_callback(result: int, relationship: Discord.Relationship) -> void:
	print(
		"Your relationship with @",
		relationship.user.username, "#", relationship.user.discriminator,
		"is currently of type ", relationship.type, "."
	)

Get At

get_at(index: int, [callback_target: Object, callback_method: String]) -> void

Get the relationship at a given index when iterating over a list of relationships. Has a corresponding signal get_at_callback that can be used instead of the optional target & method args.

Requires a filter to be set.

Arguments

  • index: int - The index in the list of relationships.

Optional Args

  • callback_target: Object - The callback target.

    callback_method: String - The callback method's name.

    get_at_callback(result: Discord.Result, relationship: Discord.Relationship)

Example

var discord: Discord.Core
var relationships: Discord.RelationshipManager


func _ready() -> void:
	# ...
	yield(relationships, "refresh")
	relationships.filter(self, "_filter")
	relationships.get_at(0, self, "_get_at_callback")


func _filter(relationship: Discord.Relationship) -> bool:
	return true


func _get_at_callback(result: int, relationship: Discord.Relationship) -> void:
	print(
		"Your relationship at index 0 is currently with @",
		relationship.user.username, "#", relationship.user.discriminator,
		"is currently of type ", relationship.type, "."
	)

Count

count([callback_target: Object, callback_method: String]) -> void

Get the number of relationships that match your filter. Has a corresponding signal count_callback that can be used instead of the optional target & method args.

Requires a filter to be set.

Optional Args

  • callback_target: Object - The callback target.

    callback_method: String - The callback method's name.

    count_callback(result: Discord.Result, count: int)

Example

var discord: Discord.Core
var relationships: Discord.RelationshipManager


func _ready() -> void:
	# ...
	yield(relationships, "refresh")
	relationships.filter(self, "_filter")

	relationships.count()
	var ret: Array = yield(relationships, "count_callback")
	var result: int = ret[0]
	var count: int = ret[1]

	if result != Discord.Result.OK:
		print("Failed to get relationships count: ", result)
		return

	for i in range(count):
		relationships.get_at(i, self, "_get_at_callback")


func _filter(relationship: Discord.Relationship) -> bool:
	return true


func _get_at_callback(result: int, relationship: Discord.Relationship) -> void:
	print(
		"@", relationship.user.username, "#", relationship.user.discriminator,
		" - ", relationship.type
	)

Signals

Refresh

refresh()

Fires at initialization when Discord has cached a snapshot of the current status of all your relationships. Wait for this to fire before calling filter within its callback.

Example

var discord: Discord.Core
var relationships: Discord.RelationshipManager


func _ready() -> void:
	# ...
	yield(relationships, "refresh")
	print("Relationships list cached!")

Relationship Update

relationship_update(relationship: Discord.Relationship)

Fires when a relationship in the filtered list changes, like an updated presence or user attribute.

Arguments

  • relationship: Discord.Relationship - The relationship that changed.

Example

var discord: Discord.Core
var relationships: Discord.RelationshipManager


func _ready() -> void:
	# ...
	relationships.connect(
		"relationship_update",
		self, "_on_relationship_update"
	)


func _on_relationship_update(relationship: Discord.Relationship) -> void:
	print(
		"Relationship with @",
		relationship.username, "#", relationship.discriminator,
		" updated to type ", relationship.type, "."
	)

Get (Callback)

get_callback(result: Discord.Result, relationship: Discord.Relationship)

Alternative to the object & method args of get.

Arguments

  • result: Discord.Result - The result of the command.

  • relationship: Discord.Relationship - The relationship between you and the user.

Get At (Callback)

get_at_callback(result: Discord.Result, relationship: Discord.Relationship)

Alternative to the object & method args of get_at.

Arguments

  • result: Discord.Result - The result of the command.

  • relationship: Discord.Relationship - The relationship between you and the user.

Count (Callback)

count_callback(result: Discord.Result, count: int)

Alternative to the object & method args of count.

Arguments

  • result: Discord.Result - The result of the command.

  • count: int - The number of relationships that match your filter.

Clone this wiki locally