我决定尝试让我的不和谐机器人播放音乐,但我已经卡住了。主要是因为我找不到任何资源来帮助当前版本,我一直在从文档中获取所有内容。但是,我不知道如何检查机器人是否已连接到语音通道。
我试过了if not Client.is_connected():,但是没有用。如果有任何更新的资源可以帮助我了解 discord.py 语音功能的基础知识,请给我一个链接:) 这是我目前的代码:
# ----- ATTEMPT AT VOICE COMMANDS ------
#discord.opus.load_opus() - what goes in bracket???
@client.command(name="join", pass_ctx=True)
async def join(ctx):
#if not is_connected(): - Client.is_connected() not working
user = ctx.message.author
vc = user.voice.channel
await vc.connect()
await ctx.send(f"Joined **{vc}**")
#else:
# await ctx.send("I'm already connected!")
@client.command(name="disconnect", pass_ctx=True)
async def disconnect(ctx):
# if not is_connected(): - once again can't work it out
vc = ctx.message.guild.voice_client # i don't even know how this worked :D
await …Run Code Online (Sandbox Code Playgroud) 我正在发出一个等待用户回复机器人的命令,但我希望机器人只接受作者的回复。
@client.command(name='numgame',
brief='Guess a number between 1 and 100',
pass_context=True)
async def numgame(context):
number = random.randint(1,100)
guess = 4
while guess != 0:
await context.send('Pick a number between 1 and 100')
msg = await client.wait_for('message', check=check, timeout=30)
attempt = int(msg.content)
if attempt > number:
await context.send(str(guess) + ' guesses left...')
await asyncio.sleep(1)
await context.send('Try going lower')
await asyncio.sleep(1)
guess -= 1
elif attempt < number:
await context.send(str(guess) + ' guesses left...')
await asyncio.sleep(1)
await context.send('Try going higher')
await asyncio.sleep(1)
guess …Run Code Online (Sandbox Code Playgroud) 在我的不和谐机器人中,我有 2 个命令可以提供和创建角色。它们工作得很好,但如果角色名称包含空格,我就会遇到问题。它将第二个单词计入第二个参数,使命令产生错误。
# Giverole
@client.command(name='giverole',
aliases=['gr'],
brief='Assgins role to a user',
pass_ctx=True)
async def giverole(ctx, rname, *, member: discord.Member):
role = get(member.guild.roles, name=rname)
await member.add_roles(role)
await ctx.send(f'Role added to user {member.mention}')
print('Giverole command executed\n- - -')
# Createrole
@client.command(name='createrole',
brief='Creates a role',
aliases=['cr','makerole'],
pass_ctx=True)
async def createrole(ctx, rname: str, clr: discord.Colour):
if ctx.author.guild_permissions.manage_roles:
await ctx.guild.create_role(name=rname, colour=clr)
await ctx.send('Role created with name: ' + rname)
print('Createrole command executed\n- - -')
else:
await ctx.send('You lack permission.')
print('Createrole command executed\n- - -') …Run Code Online (Sandbox Code Playgroud) 我试图通过 向我的 GUI 显示图像PhotoImage,但是它告诉我 PhotoImage 没有“网格”成员。
我正在使用.grid()所有其他小部件,例如标签和按钮,所以我不能使用.pack(),如果这会产生影响的话。那么,如何使用网格在 GUI 上显示图像呢?
我不知道这个问题是否需要代码,但是
# it works by getting a random image from a list of files
Label(root, text=f"Enter a number between one and {len(files)}")
self.entry = Entry(root)
self.entry.grid()
Button(root, text="Submit", command=self.getEntry).grid()
Run Code Online (Sandbox Code Playgroud)
那么,getEntry() 是:
def getEntry(self):
PhotoImage(name="",
file=f{self.path}\\{self.showImage(self.entry.get())}).grid(row=1)
"""
showImage() just returns the correct file
(e.g: files[index] where index is the input)
"""
Run Code Online (Sandbox Code Playgroud) 好的,我正在编写一个命令,它仅显示有关用户正在收听的歌曲的一些信息,例如k!spotify @user显示歌曲名称、艺术家、专辑、专辑封面等。但是,我在 Spotify 类方面遇到问题。
这是我第一次尝试使用 Spotify 类,但我认为这不是问题。我希望这些变量是特定于用户的,而不是<property object at 0x0460B7E0>:
async def spotify(ctx, user: discord.Member=None):
if not user:
user = ctx.message.author.id
else:
user = user.id
sname = discord.Spotify.title
sartists = discord.Spotify.artists
album = discord.Spotify.album
palbum = discord.Spotify.album_cover_url
duration = discord.Spotify.duration
Run Code Online (Sandbox Code Playgroud)
我知道discord.Spotify.title等不会给我一些特定于用户的东西,但我尝试user.Spotify.title了这些,但没有成功。我确信这只是我的一个误解,但是我应该如何在这里使用 Spotify 类呢?