当我在我的Discord服务器聊天中输入/
时,命令不显示。我什么都试过了,不知道为什么都不行。以下是bot的代码:
import discord
import asyncio
from discord.ext import commands, tasks
import os
import random
from discord.ext import commands
from discord.utils import get
from discord import FFmpegPCMAudio
from discord import TextChannel
from youtube_dl import YoutubeDL
import sys
import spotipy
import spotipy.util as util
import youtube_dl
intents = discord.Intents.all()
bot = commands.Bot(command_prefix=".", intents=intents)
# Set the intents for the bot
intents.members = True
intents.presences = True
intents.typing = True
intents.message_content = True
client = commands.Bot(command_prefix='.', intents=intents)
audio_data = None
CLIENT_ID = "YOUR_ID"
CLIENT_SECRET = "SECRET"
REDIRECT_URI = "http://localhost:8888/callback"
USERNAME = "your-username"
scope = "user-read-private user-read-playback-state user-modify-playback-state"
token = util.prompt_for_user_token(USERNAME, scope, client_id=CLIENT_ID, client_secret=CLIENT_SECRET, redirect_uri=REDIRECT_URI)
spotify_api = spotipy.Spotify(auth=token)
players = {}
@client.event # check if bot is ready
async def on_ready():
print('Bot online')
@client.command()
async def entra(ctx):
channel = ctx.message.author.voice.channel
voice = get(client.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
@client.command(name='leave', aliases=['esci', 'quit'], pass_context=True)
async def leave(ctx):
voice_client = ctx.voice_client
if voice_client is not None:
await voice_client.disconnect()
await ctx.send('🤙')
else:
await ctx.send('😐')
@client.command(name='avvia', aliases=['ascolta', 'play'], description="riproduce link youtube")
async def play(ctx, url):
channel = ctx.message.author.voice.channel
voice = get(client.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
YDL_OPTIONS = {'format': 'bestaudio', 'noplaylist': 'True'}
FFMPEG_OPTIONS = {
'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
voice = get(client.voice_clients, guild=ctx.guild)
# Check if the given url is a Spotify track or playlist
if "open.spotify.com/track/" in url:
# Extract the track id from the url
track_id = url.split("/")[-1]
# Get the track data from Spotify
track_data = spotify_api.track(track_id)
# Set the audio data to the track's preview url
audio_data = track_data["preview_url"]
await ctx.send("è possibile solo sentire i 30 secondi di preview di una canzone tramite link di spotify perche spotify è stronzo 😐")
elif "open.spotify.com/playlist/" in url:
# Extract the playlist id from the url
playlist_id = url.split("/")[-1]
# Get the playlist data from Spotify
playlist_data = spotify_api.playlist(playlist_id)
# Get the playlist's track data
track_data = spotify_api.playlist_tracks(playlist_id)
# Set the audio data to the first track's preview url
audio_data = track_data[0]["preview_url"]
await ctx.send(
"è possibile solo sentire i 30 secondi di preview di una canzone tramite link di spotify perche spotify è stronzo 😐")
elif "youtube.com" in url:
# The url is not a Spotify track or playlist, so use YoutubeDL to extract the audio data
with YoutubeDL(YDL_OPTIONS) as ydl:
info = ydl.extract_info(url, download=False)
audio_data = info['url']
else:
await ctx.send('😐')
if not voice.is_playing():
# Play the audio data
voice.play(FFmpegPCMAudio(audio_data, **FFMPEG_OPTIONS))
voice.is_playing()
if ctx.message.author == "Aq3ila":
await ctx.send("musica di merda incoming 💀💀💀")
else:
await ctx.send("🎵")
return
else:
await ctx.send("impara ad'aspettare")
# command to resume voice if it is paused
@client.command()
async def riavvia(ctx):
voice = get(client.voice_clients, guild=ctx.guild)
if not voice.is_playing():
voice.resume()
await ctx.send('riavviando')
# command to pause voice if it is playing
@client.command()
async def pausa(ctx):
voice = get(client.voice_clients, guild=ctx.guild)
if voice.is_playing():
voice.pause()
await ctx.send('messo in pausa')
# command to stop voice
@client.command(name='ferma', aliases=['stop', 'annulla'], description="ferma la canzone in riproduzione")
async def stop(ctx):
voice = get(client.voice_clients, guild=ctx.guild)
if voice.is_playing():
voice.stop()
await ctx.send('💥💢💥💢')
client.run("YOUR_TOKEN")
我不知道为什么它不工作。
首先,您已经将Bot令牌包含在bot.run()
中。马上删除并重置你的令牌。
下一个:你所有的命令都是文本命令,也就是说,当消息中有bot前缀时,它们会被触发。
斜杠命令集成到Discord客户端本身。它们不需要消息来触发,但可以直接从客户端运行。
我将向您展示我用来创建斜杠命令的方法,但是还有其他方法可以做到这一点。
要创建斜杠命令,首先在on_ready中添加:
@bot.event
async def on_ready():
#sync your commands and saves information about them in a var
synced = await bot.tree.sync()
#print how many commands were synced.
print(f"Synced {len(synced)} command(s)")
之后,用@bot.tree.command
装饰器创建命令。
例如:
@bot.tree.command(name="command_nam", description="command_description")
async def unique_command_name(interaction: discord.Interaction):
#code here
这里,您使用的不是Context或ctx,而是Interaction类。你可以在文档中查看交互是如何工作的。
编辑:正如下面的评论所述,不要对那些有很多命令需要你经常重启的bot这样做。它只是非通用bot的一种替代方法。