@@ -47,12 +47,15 @@ async def connect_nodes():
47
47
""" Connect to our Lavalink nodes."""
48
48
await bot.wait_until_ready() # wait until the bot is ready
49
49
50
- await wavelink.NodePool.create_node(
51
- bot = bot,
52
- host = ' 0.0.0.0' ,
53
- port = 2333 ,
54
- password = ' youshallnotpass'
55
- ) # create a node
50
+ nodes = [
51
+ wavelink.Node(
52
+ identifier = " Node1" , # This identifier must be unique for all the nodes you are going to use
53
+ uri = " http://0.0.0.0:443" , # Protocol (http/s) is required, port must be 443 as it is the one lavalink uses
54
+ password = " youshallnotpass"
55
+ )
56
+ ]
57
+
58
+ await wavelink.Pool.connect(nodes = nodes, client = bot) # Connect our nodes
56
59
```
57
60
58
61
<br />
@@ -67,23 +70,35 @@ Now you are finished making your node! Next, you will want to:
67
70
To make a play command, you will need to make a function to connect and play audio in a voice channel.
68
71
69
72
``` py title="Play Command Example"
73
+ import typing
74
+
70
75
@bot.slash_command (name = " play" )
71
76
async def play (ctx , search : str ):
72
- vc = ctx.voice_client # define our voice client
73
-
74
- if not vc: # check if the bot is not in a voice channel
75
- vc = await ctx.author.voice.channel.connect(cls = wavelink.Player) # connect to the voice channel
76
-
77
- if ctx.author.voice.channel.id != vc.channel.id: # check if the bot is not in the voice channel
78
- return await ctx.respond(" You must be in the same voice channel as the bot." ) # return an error message
79
-
80
- song = await wavelink.YouTubeTrack.search(query = search, return_first = True ) # search for the song
81
-
82
- if not song: # check if the song is not found
83
- return await ctx.respond(" No song found." ) # return an error message
84
-
85
- await vc.play(song) # play the song
86
- await ctx.respond(f " Now playing: ` { vc.source.title} ` " ) # return a message
77
+ # First we may define our voice client,
78
+ # for this, we are going to use typing.cast()
79
+ # function just for the type checker know that
80
+ # `ctx.voice_client` is going to be from type
81
+ # `wavelink.Player`
82
+ vc = typing.cast(wavelink.Player, ctx.voice_client)
83
+
84
+ if not vc: # We firstly check if there is a voice client
85
+ vc = await ctx.author.voice.channel.connect(cls = wavelink.Player) # If there isn't, we connect it to the channel
86
+
87
+ # Now we are going to check if the invoker of the command
88
+ # is in the same voice channel than the voice client, when defined.
89
+ # If not, we return an error message.
90
+ if ctx.author.voice.channel.id != vc.channel.id:
91
+ return await ctx.respond(" You must be in the same voice channel as the bot." )
92
+
93
+ # Now we search for the song. You can optionally
94
+ # pass the "source" keyword, of type "wavelink.TrackSource"
95
+ song = await wavelink.Playable.search(search)
96
+
97
+ if not song: # In case the song is not found
98
+ return await ctx.respond(" No song found." ) # we return an error message
99
+
100
+ await vc.play(song) # Else, we play it
101
+ await ctx.respond(f " Now playing: ` { song.title} ` " ) # and return a success message
87
102
```
88
103
89
104
<DiscordComponent >
@@ -113,8 +128,11 @@ async def on_ready():
113
128
await connect_nodes() # connect to the server
114
129
115
130
@bot.event
116
- async def on_wavelink_node_ready (node : wavelink.Node):
117
- print (f " { node.identifier} is ready. " ) # print a message
131
+ async def on_wavelink_node_ready (payload : wavelink.NodeReadyEventPayload):
132
+ # Everytime a node is successfully connected, we
133
+ # will print a message letting it know.
134
+ print (f " Node with ID { payload.session_id} has connected " )
135
+ print (f " Resumed session: { payload.resumed} " )
118
136
119
137
bot.run(" token" )
120
138
```
0 commit comments