如何使用不和谐机器人从列表中删除条目
How to remove an entry from a list with a discord bot
我有一个支持播放列表的音乐机器人。为此,我制作了一个显示队列的 playlist
命令。我还有一个清除整个队列的命令。现在我想实现从列表中按编号删除一首歌曲的可能性。我将如何重新编写以下命令?
@commands.cooldown(1, 5, BucketType.guild)
@commands.command(aliases=["delplaylist"])
@commands.guild_only()
@commands.check(audio_playing)
async def remove(self, ctx):
"""Deletes a single song from the playlist."""
state = self.get_state(ctx.guild)
entries = []
for (index, song) in enumerate(state.playlist):
entries.append(
(f"**[{index + 1}] {song.title}** *by* {song.uploader}", f"requested by {song.requested_by.mention}"))
pages = PlaylistPages(ctx, entries)
return await pages.paginate()
我知道代码只显示播放列表本身。我将从获取 entries
开始,然后按编号删除它们,但这是否可能以这种“简单”的方式进行?
按索引删除:
使用 del
keyword
>>> lst = ['elem1', 'elem2', 'elem3', 'elem4', 'elem5']
>>> del lst[0] # The index of the element
>>> lst
['elem2', 'elem3', 'elem4', 'elem5']
使用list.pop
>>> lst = ['elem1', 'elem2', 'elem3', 'elem4', 'elem5']
>>> lst.pop(0) # It also returns the value
'elem1'
>>> lst
['elem2', 'elem3', 'elem4', 'elem5']
我有一个支持播放列表的音乐机器人。为此,我制作了一个显示队列的 playlist
命令。我还有一个清除整个队列的命令。现在我想实现从列表中按编号删除一首歌曲的可能性。我将如何重新编写以下命令?
@commands.cooldown(1, 5, BucketType.guild)
@commands.command(aliases=["delplaylist"])
@commands.guild_only()
@commands.check(audio_playing)
async def remove(self, ctx):
"""Deletes a single song from the playlist."""
state = self.get_state(ctx.guild)
entries = []
for (index, song) in enumerate(state.playlist):
entries.append(
(f"**[{index + 1}] {song.title}** *by* {song.uploader}", f"requested by {song.requested_by.mention}"))
pages = PlaylistPages(ctx, entries)
return await pages.paginate()
我知道代码只显示播放列表本身。我将从获取 entries
开始,然后按编号删除它们,但这是否可能以这种“简单”的方式进行?
按索引删除:
使用 del
keyword
>>> lst = ['elem1', 'elem2', 'elem3', 'elem4', 'elem5']
>>> del lst[0] # The index of the element
>>> lst
['elem2', 'elem3', 'elem4', 'elem5']
使用list.pop
>>> lst = ['elem1', 'elem2', 'elem3', 'elem4', 'elem5']
>>> lst.pop(0) # It also returns the value
'elem1'
>>> lst
['elem2', 'elem3', 'elem4', 'elem5']