-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic_bot.py
More file actions
195 lines (139 loc) · 5.11 KB
/
Copy pathbasic_bot.py
File metadata and controls
195 lines (139 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
"""Basic Discord music bot using FluxWave.
Environment:
DISCORD_TOKEN Discord bot token.
LAVALINK_URI Lavalink base URI, e.g. http://127.0.0.1:2333.
LAVALINK_PASSWORD Lavalink password.
Run:
python examples/basic_bot.py
"""
from __future__ import annotations
import os
import discord
from discord.ext import commands
import fluxwave
def required_env(name: str) -> str:
value = os.getenv(name)
if not value:
raise RuntimeError(f"Missing required environment variable: {name}")
return value
intents = discord.Intents.default()
intents.message_content = True
intents.voice_states = True
bot = commands.Bot(command_prefix="!", intents=intents)
@bot.event
async def setup_hook() -> None:
node = fluxwave.Node(
uri=os.getenv("LAVALINK_URI", "http://127.0.0.1:2333"),
password=required_env("LAVALINK_PASSWORD"),
user_id=bot.user.id,
identifier="main",
search_cache_capacity=256,
)
await fluxwave.Pool.connect(nodes=[node], cache_capacity=512)
@bot.event
async def on_ready() -> None:
assert bot.user is not None
print(f"Logged in as {bot.user} | FluxWave {fluxwave.__version__}")
@bot.event
async def on_fluxwave_track_start(event: fluxwave.TrackStartEvent) -> None:
print(f"Started: {event.track.title} in guild {event.guild_id}")
@bot.event
async def on_fluxwave_track_end(event: fluxwave.TrackEndEvent) -> None:
print(f"Ended: {event.track.title} ({event.reason})")
async def get_player(ctx: commands.Context) -> fluxwave.FluxPlayer | None:
voice = getattr(ctx.author, "voice", None)
channel = getattr(voice, "channel", None)
if channel is None:
await ctx.reply("Join a voice channel first.")
return None
if isinstance(ctx.voice_client, fluxwave.FluxPlayer):
return ctx.voice_client
return await channel.connect(cls=fluxwave.FluxPlayer)
@bot.command()
async def join(ctx: commands.Context) -> None:
player = await get_player(ctx)
if player is not None:
await ctx.reply(f"Connected to {player.channel}.")
@bot.command()
async def play(ctx: commands.Context, *, query: str) -> None:
player = await get_player(ctx)
if player is None:
return
result = await player.enqueue(query)
if result.added == 0:
await ctx.reply("No tracks found.")
return
if player.current is None:
await player.skip(force=True)
await ctx.reply(result.message)
@bot.command()
async def playnext(ctx: commands.Context, *, query: str) -> None:
player = await get_player(ctx)
if player is None:
return
result = await player.play_next(query)
await ctx.reply(result.message if result.added else "No tracks found.")
@bot.command()
async def skip(ctx: commands.Context) -> None:
player = ctx.voice_client
if not isinstance(player, fluxwave.FluxPlayer):
await ctx.reply("Not connected.")
return
skipped = await player.skip(force=True)
await ctx.reply(f"Skipped {skipped.title}." if skipped else "Nothing to skip.")
@bot.command()
async def stop(ctx: commands.Context) -> None:
player = ctx.voice_client
if not isinstance(player, fluxwave.FluxPlayer):
await ctx.reply("Not connected.")
return
await player.stop(force=True)
await ctx.reply("Stopped playback and cleared the queue and autoplay recommendations.")
@bot.command()
async def pause(ctx: commands.Context) -> None:
player = ctx.voice_client
if isinstance(player, fluxwave.FluxPlayer):
await player.pause()
await ctx.reply("Paused.")
@bot.command()
async def resume(ctx: commands.Context) -> None:
player = ctx.voice_client
if isinstance(player, fluxwave.FluxPlayer):
await player.resume()
await ctx.reply("Resumed.")
@bot.command()
async def volume(ctx: commands.Context, value: int) -> None:
player = ctx.voice_client
if isinstance(player, fluxwave.FluxPlayer):
await player.set_volume(value)
await ctx.reply(f"Volume set to {player.volume}.")
@bot.command(name="queue")
async def queue_command(ctx: commands.Context) -> None:
player = ctx.voice_client
if not isinstance(player, fluxwave.FluxPlayer):
await ctx.reply("Not connected.")
return
if not player.queue:
await ctx.reply("Queue is empty.")
return
lines = [
f"{index}. {track.title} - {track.author}"
for index, track in enumerate(player.queue[:10], start=1)
]
await ctx.reply("\n".join(lines))
@bot.command()
async def now(ctx: commands.Context) -> None:
player = ctx.voice_client
if not isinstance(player, fluxwave.FluxPlayer) or player.current is None:
await ctx.reply("Nothing is playing.")
return
track = player.current
await ctx.reply(f"Now playing: {track.title} - {track.author} [{track.length_display}]")
@bot.command()
async def leave(ctx: commands.Context) -> None:
player = ctx.voice_client
if isinstance(player, fluxwave.FluxPlayer):
await player.disconnect(force=True)
await ctx.reply("Disconnected.")
if __name__ == "__main__":
bot.run(required_env("DISCORD_TOKEN"))