How can I make a discord bot take a message and turn it into a varable?

Question:

Please be forgivable with my code, this is my first ever project with py. I’d like it to turn a message user like "/channel http://youtube.com@youtube" to a variable like "channelID" that can be used later in the py.
I’m working with this so far.


import discord
import re
import easygui
from easygui import *
from re import search
from urllib.request import urlopen
from bs4 import BeautifulSoup
import requests
import re
import json
import base64
import os
import webbrowser
import pyperclip
import win32com.client as comclt
import time
import pyautogui
import discord
 
intents = discord.Intents.all()
client = discord.Client(command_prefix='!', intents=intents)
 
@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))
client.run('token')
@client.event
async def on_message(message):
    if message.author == client.user:
        return
 
    if message.content.startswith('/channel '):
        channelURL = message.content()
 


        if search("http", channelURL):
            if re.search("://", channelURL):
                if re.search("youtu", channelURL):
                    
                    # Loads page data #
                    soup = BeautifulSoup(requests.get(channelURL, cookies={'CONSENT': 'YES+1'}).text, "html.parser")
                    data = re.search(r"var ytInitialData = ({.*});", str(soup.prettify())).group(1)
                    json_data = json.loads(data)
                    
                    # Finds channel information #
                    channel_id   = json_data["header"]["c4TabbedHeaderRenderer"]["channelId"]
                    channel_name = json_data["header"]["c4TabbedHeaderRenderer"]["title"]
                    channel_logo = json_data["header"]["c4TabbedHeaderRenderer"]["avatar"]["thumbnails"][2]["url"]
                    channel_id_link = "https://youtube.com/channel/"+channel_id
                    
                    
                    # Prints Channel information to console #
                    print("Channel ID: "+channel_id)
                    print("Channel Name: "+channel_name)
                    print("Channel Logo: "+channel_logo)
                    print("Channel ID: "+channel_id_link)
                    
                    # Creates HTML file var# 
                    f = open('channelid.html','w')
                    
                
                    # Converts and downlaods image file to png # 
                    imgUrl = channel_logo
                    filename = "image.png".split('/')[-1]
                    r = requests.get(imgUrl, allow_redirects=True)
                    open(filename, 'wb').write(r.content)
                    
        await message.channel.send(channel_id_link)


I tried to use

if message.content.startswith('/channel '):

and

channelURL = message.content()

But I know I’m missing something really simple. I just can’t put my finger on it.

I was channelURL = message.content() to store the variable of if message.content.startswith('/channel '):

Asked By: flyinggoatman

||

Answers:

Can you please specify what exactly happens when you try to retrieve the message content? do you get an empty string? you can test it out by just trying to print message.content() when the function calls.

I’m guessing you may have forgotten to enable message intents from the discord developer portal if you’re receiving an empty string. You can enable this by going to your application on discord developer portal, select Bot from the sidebar and scroll down until you see a message content intent it looks like this

Answered By: Yaseen

discord.Message.content is a str and can’t be called like a function i.e., message.content().

Use channelURL = message.content instead.
Remember to enable message content intents.

Answered By: sagamantus
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.