How to change a channels name using discord.py v.1

2020-02-13 08:13发布

问题:

i've already searched a lot about this online. There I came across the API Refrence (https://discordpy.readthedocs.io/en/rewrite/api.html#discord.TextChannel) which kind of helped me finding what command I need to use. So my conculsion would be to use this code:

channel = client.get_channel(475772135730708480)
@client.command()
async def emoivb(ctx):
    await discord.VoiceChannel.edit(channel, name = "test")

the problem is that it doesnt work with this error:

File "C:/Users/MyUser/Desktop/discordbot.py", line 25, in emoivb
    await discord.VoiceChannel.edit(channel, name = "test")

So this error doesnt help me at all... but im sure I just didnt understand the API refrence correctly and didnt use the command as it is supposed to be used. Im pretty new to python coding so that is the most likely thing to have happended. If anyone has some more python knowledge and is able to understand what I did wrong I would really appreciate your help! :)

回答1:

The easier way would be to specify the target channel and name in the command, something like

@client.command()
async def emoivb(ctx, channel: discord.VoiceChannel, *, new_name):
    await channel.edit(name=new_name)

This isn't perfect though: For names with spaces you'll have to enclose the existing channel name in quotes

!rename "Old Channel" New Channel

This is because Discord doesn't support mentioning voice channels.