bot.py

mail@pastecode.io avatar
unknown
python
16 days ago
2.4 kB
3
Indexable
Never
# bot.py
import nextcord
from nextcord import Intents, utils
from nextcord.ext import commands
from flask import Flask, request, jsonify
import multiprocessing
import requests

# Initialize Discord bot
intents = Intents.default()
intents.members = True
bot = commands.Bot(command_prefix='!', intents=intents)

# Role IDs (Replace these with your actual role IDs)
ROLE_PARTNER = 705129728234881086
ROLE_AFFILIATE = 705130154904518756
ROLE_CREATOR = 705225579959418931

# Replace this with your Discord server ID
GUILD_ID = 705123157438234734

# Flask App
flask_app = Flask(__name__)

@flask_app.route('/assign_role', methods=['POST'])
def assign_role():
    data = request.json
    broadcaster_type = data.get('broadcaster_type')
    discord_user_id = data.get('discord_user_id')

    guild = bot.get_guild(GUILD_ID)
    if not guild:
        return jsonify({'status': 'error', 'message': 'Guild not found'}), 404

    member = guild.get_member(int(discord_user_id))
    if not member:
        return jsonify({'status': 'error', 'message': 'Member not found'}), 404

    role_to_assign = None
    if broadcaster_type == 'partner':
        role_to_assign = utils.get(guild.roles, id=ROLE_PARTNER)
    elif broadcaster_type == 'affiliate':
        role_to_assign = utils.get(guild.roles, id=ROLE_AFFILIATE)
    else:
        role_to_assign = utils.get(guild.roles, id=ROLE_CREATOR)

    if not role_to_assign:
        return jsonify({'status': 'error', 'message': 'Role not found'}), 404

    # Check if the bot can assign the role
    if role_to_assign.position >= bot.user.top_role.position:
        return jsonify({'status': 'error', 'message': 'Bot cannot assign this role'}), 403

    try:
        bot.loop.create_task(member.add_roles(role_to_assign))
        return jsonify({'status': 'success', 'message': 'Role assigned successfully'})
    except Exception as e:
        return jsonify({'status': 'error', 'message': str(e)}), 500

def run_flask():
    flask_app.run(host='0.0.0.0', port=5002)  # Ensure this port is not in use

@bot.event
async def on_ready():
    print(f'Logged in as {bot.user.name}')

if __name__ == '__main__':
    # Start the Flask app and the Discord bot in separate processes
    flask_process = multiprocessing.Process(target=run_flask)
    flask_process.start()
    bot.run('YOUR_DISCORD_BOT_TOKEN')  # Replace with your actual bot token
Leave a Comment