2021-04-29

Discord.py Multi Ban

So I know that there are other solutions to solve this, but I want to incorporate the actual function into my on_message decorator. I can't get anything to work. I started with creating a parser to parse through the message so: my.ban @Tim @Tony @Trent --> ['tims_id',"tony's_id",'trents_id'] which worked.

Then I plugged it into a function I made which is supposed to return a list of member objects with those id's with client.get_user(user_id) which was buggy.

Finally the part that is causing the most trouble is actually running a ban command, not only is my command not working, but it's not taking the member object as a argument. Here is all of the code that pertains to these issues.

If someone could give me some pointers in the right direction or just offer a different approach in general, that would be greatly appreciated.

PARSER

def get_ids(message):
    message = message.replace("<","");message = message.replace(">","");message = message.replace("@","");message = message.replace("!","")
    message = message.split()
    for thing in message:
        if thing.isdigit():
            message[message.index(thing)] = message[message.index(thing)] + " "
            continue
        else:
            del message[message.index(thing)]
    message[0] = message[0] + " "
    message = "".join(message)
    message = message.split(" ")
    del message[-1];
    return message

PROPER COMMAND SYNTAX RECOGNITION

def is_calling_command(message_content,*command_names,current_channel=None,allowed_channel=None,prefix="my."):
    command_names = [x for x in command_names]
    for x in range(len(command_names)):
        command_names[x] = command_names[x].lower()
    message_content = message_content.lower()
    
    if current_channel is None and allowed_channel is None:
            for command_name in command_names:
                if not (prefix + command_name in message_content):
                    return False
            return True
    elif current_channel == allowed_channel:
        for command_name in command_names:
            if not (prefix + command_name in message_content):
                return False
        return True                 
    
    else:
        return False

ID TO OBJECT CONVERTER

def get_member_objects(message):
        objects = []
        ids = get_ids(message)
        for id in ids:
            objects.append(client.get_user(id))
        return objects

BAN COMMAND

@client.event
async def on_message(message):
if is_calling_command(message.content,"ban",current_channel=message.channel.id,allowed_channel=message.channel.id) and message.channel.permissions_for(message.author).administrator:
        connection = client.get_channel(message.channel.id)
        print(get_member_objects(message.content))
        for obj in get_member_objects(message.content):
            await client.ban(obj, reason="cause we said so")
            await connection.send("Banned!")

Here is the error message I'm getting [ERR_MSG][1] [1]: https://ift.tt/3gLPtrb



from Recent Questions - Stack Overflow https://ift.tt/3aNbKRJ
https://ift.tt/eA8V8J

No comments:

Post a Comment