如何发出使我的机器人离开特定服务器的命令?

时间:2020-09-07 08:09:49

标签: python-3.x discord.py-rewrite

我想使用诸如-svleave之类的命令以及服务器的名称或ID,然后使用漫游器离开该服务器。我在Google上搜索了如何执行此操作,但我只收到了这篇帖子How to make a discord bot leave a server from a command in another server?,它没有向我展示如何将其作为类似@client.command()的命令来实现,但我看不到如何添加它作为命令。

from discord.ext import commands
import discord.utils

client = commands.Bot(command_prefix = "-")

@client.event
async def on_ready():
    print('Ahri is Ready!')
    await client.change_presence(status=discord.Status.online, activity=discord.Game("cu pizda lui ma-ta"))
    

@client.command()
async def ping(ctx):
    await ctx.send(f'Ping: {round(client.latency * 1000)}')

################################################################
#                          ADMIN                               #
################################################################

@client.command(pass_context=True) # Send Message As Bot
@commands.has_permissions(administrator=True)
async def sudo(ctx, *, message):
    await ctx.message.delete()
    await ctx.send(message)

@client.command() # Clears Messages
@commands.has_permissions(manage_messages=True)
async def clear(ctx, amount=5):
    await ctx.channel.purge(limit=amount)

@client.command() # Kicks Members
@commands.has_permissions(kick_members=True)
async def kick(ctx, member : discord.Member, *, reason=None):
    await member.kick(reason=reason)
    await ctx.send(f"Kicked {member.mention}")

@client.command() # Bans Members
@commands.has_permissions(ban_members=True)
async def ban(ctx, member : discord.Member, *, reason=None):
    await member.ban(reason=reason)
    await ctx.send(f"Banned {member.mention}")

@client.command() # Unbans Members
@commands.has_permissions(ban_members=True)
async def unban(ctx, *, member):
    banned_users= await ctx.guild.bans()
    member_name, member_discriminator = member.split('#')

    for ban_entry in banned_users:
        user = ban_entry.user

        if (user.name, user.discriminator) == (member_name, member_discriminator):
            await ctx.guild.unban(user)
            await ctx.send(f"Unbanned {user.mention}")
            return

################################################################
#                          ROLES                               #
################################################################

role = "Newcomer"

@client.event
async def on_member_join(member):
    rank = discord.utils.get(member.guild.roles, name = role)
    await member.add_roles(rank)

@client.command()
@commands.is_owner()
async def leaveguild(self, ctx, *, guild: discord.Guild):
    await guild.leave()
    await ctx.send(f"I've left {guild.name}!")
    

client.run("")

1 个答案:

答案 0 :(得分:0)

下面的代码允许您输入公会的名称或ID来离开公会,并确保您(机器人的所有者)是运行该命令的人。

您可以阅读更多here

import discord
from discord.ext import commands

@client.command()
@commands.is_owner()
async def leaveguild(ctx, *, guild: discord.Guild):
    await guild.leave()
    await ctx.send(f"I've left {guild.name}!")