Discord.py массовый dm-бот

Я пытался найти способ сделать так, чтобы бот разорвал всех на моем сервере. Я уже нашел вопрос, похожий на мой, и попробовал ответить, но это не сработало. Мой текущий код выглядит так

  if message.content.upper().startswith('.MSG'):
      if "345897570268086277" in [role.id for role in message.author.roles]:
        member = discord.Member
        args = message.content.split(" ")
        if member == "@everyone":
          for server_member in server.members:
          await client.send_message(server_member, "%s" % (" ".join(args[1:])))

person Raymond Zhu    schedule 15.03.2018    source источник


Ответы (1)


Я бы использовал для этого расширение команд, а не on_message

# import stuff we'll be using
from discord.ext import commands
from discord.utils import get

# Bot objects listen for commands on the channels they can "see" and react to them by 
# calling the function with the same name

bot = commands.Bot(command_prefix='.')

# Here we register the below function with the bot Bot
# We also specify that we want to pass information about the message containing the command
# This is how we identify the author
@bot.command(pass_context=True) 
# The command is named MSG
# The `, *, payload` means take everything after the command and put it in one big string
async def MSG(ctx, *, payload):   
    # get will return the role if the author has it.  Otherwise, it will return None
    if get(ctx.message.author.roles, id="345897570268086277"): 
        for member in ctx.message.server.members:
            await bot.send_message(member, payload)

bot.run('token')

Насколько вы уверены, что "345897570268086277" - это идентификатор роли? Возможно, имеет смысл искать его по имени.

person Patrick Haugh    schedule 15.03.2018
comment
Могу добавить несколько комментариев. Возможно, вам будет полезно прочитать документацию по discord.ext.commands в это время. Документация предназначена для экспериментальной ветки, но общая структура идентична. - person Patrick Haugh; 15.03.2018
comment
Спасибо, но когда я заменил свой старый код и попробовал, ничего не вышло, не могли бы вы рассказать мне, что делает каждая часть вашего кода, потому что это поможет мне понять, в чем может быть проблема. - person Raymond Zhu; 15.03.2018
comment
Да, это поможет. - person Raymond Zhu; 15.03.2018
comment
Я добавил несколько комментариев. Спросите, есть ли у вас еще вопросы. Я также забыл добавить bot.run к исходной версии, поэтому он никогда не запускал бота. - person Patrick Haugh; 15.03.2018
comment
В моем коде я использовал client.run('token'), а в начале вместо bot = commands.Bot(commands_prefix='.') я использовал client = commands.Bot(command_prefix='.'), будет ли он работать так же? - person Raymond Zhu; 15.03.2018
comment
если вы также замените все другие ссылки на объект bot, да. - person Patrick Haugh; 15.03.2018
comment
Как только я запускаю его, появляется сообщение об ошибке discord.errors.Forbidden: FORBIDDEN (код состояния: 403): невозможно отправлять сообщения этому пользователю. - person Raymond Zhu; 15.03.2018