0
votes

first post! yay... so. im making a bot and ran into some trouble.

i cant seem to get my bot to post a web-parsed material in my discord channel upon calling the "stats" command i wrote. i get "403 unauthorized" upon calling the command! im trying to display some stats.

python used:

import asyncio
import datetime
from difflib import SequenceMatcher
import json
import urllib
from bs4 import BeautifulSoup
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import requests
import html2text
from html.parser import HTMLParser
from html.entities import name2codepoint
client = discord.Client()

bot = commands.Bot(command_prefix='!', description='a halo server status check bot.')
#responce = requests.get(url)
#responce.text[:100]
import urllib.request

url = "http://chiperdilly.com/cestatstext/test.html"
html = urllib.request.urlopen(url).read()
mystr = html.decode("utf8")

soup = BeautifulSoup(html, "html.parser")
stringdata = (element.text for element in soup.find_all("div", "value"))
chanel = client.get_channel("437430094445674500")
for script in soup(["script", "style"]):
    script.extract

@bot.event
async def on_ready():
    print('dont let dreams be dreams...')
    print(bot.user.name)
    print(bot.user.id)

@bot.event
async def on_message(message):
    if message.content.startswith('!stats'):
        #await client.send_message(message.channel, content=stringdata)
        bot.say(message.channel, stringdata)
        #print(stringdata)
bot.run(myTOKEN)

html used:

<!DOCTYPE html>
<html>
    <head>
        <title>Halo CE Server Stats tool</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <meta http-equiv="refresh" content="10">
        <style type="text/css">
        </style>

        <link href="css/rhp_customized.css" rel="stylesheet" type="text/css">
    </head>
    <body background="images/hp_bg.jpg">
        <table width="699" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td><img src="images/hp_top.jpg" width="699" height="58" /></td>
  </tr>
  <tr>
    <td background="images/hp_tablebg.jpg"><img src="images/hp_serverinfo.jpg" width="699" height="44" /></td>
  </tr>
  <tr>
    <td background="images/hp_tablebg.jpg" style="padding-left: 35px; padding-right: 50px;">
      <table width="600" border="0" align="center" cellpadding="3" cellspacing="1">
        <tr>
          <td valign="top">
            <div align="center" class="value"><strong><font size="+1">!tht_serber!</font></strong><br />
            <span class="smalltext" class="value">Dedicated  server v01.00.10.0621</span><br /><br />
            </div>
            <table width="100%">
              <tr>
                <td>
                  Address: <strong><a href="halogame.com:2302">halogame.com:2302</a></strong><br />
                  This is a <strong>public</strong> server<br />
                  There are <b>1</b> / <b>16</b> players<br />
                  Current game is <b>CTF</b> (CTF)<br />
                  The Frag/Score Limit is <b>3</b>
                </td>
                <td valign="top">
                  <div align="center" class="value"><strong>Team Scores:</strong><br />
                  <span class="red"><b>Red Team: 0</b></span><br /><span class="blue"><b>Blue Team: 0</b></span></div>
                </td>
              </tr>
            </table>
            </div>
          </td>
          <td>
            <div align="center" class="value"><a href="http://hce.halomaps.org/index.cfm?search=Bloodgulch"><img src="images/maps/bloodgulch.jpg" width="200" height="150" id="map_illustration" alt="Halo Bigtits map"></a><br />
            <strong>Bloodgulch</strong></div><br />
          </td>
        </tr>
      </table>
    </td>
  </tr>
  <tr>
    <td background="images/hp_tablebg.jpg"><img src="images/hp_playerinfo.jpg" width="699" height="47" /></td>
  </tr>
  <tr>
    <td background="images/hp_tablebg.jpg" style="padding-left: 35px; padding-right: 50px;"><table width="100%" border="0" cellpadding="3">
    <tr>
        <td width="33%" class="bigbold" align="center">
            Player Name
        </td>
        <td width="33%" class="bigbold" align="center">
            Score
        </td>
        <td width="34%" class="bigbold" align="center">
            Team
        </td>
    </tr>
    <tr>
        <td width="33%" align="center">
            somedude
        </td>
        <td width="33%" align="center">
            0
        </td>
        <td width="34%" align="center">
            <span class="red"><strong>RED</strong></span>
        </td>
    </tr>
</table></td>
  </tr>
  <tr>
    <td><img src="images/hp_bottom.jpg" width="699" height="49" /></td>
  </tr>
</table>
        <br />

    </body>
</html>

does anyone know how i can get past the http 403 issue? and get my code to paste the table values into the chat?

EDIT: got past 403 now stuck on 401:

Traceback (most recent call last): File "C:\Python36_64\lib\site-packages\discord\client.py", line 307, in _run_event yield from getattr(self, event)(*args, **kwargs) File "halobot.py", line 43, in on_message await client.send_message(message.channel, content=stringdata) File "C:\Python36_64\lib\site-packages\discord\client.py", line 1152, in send_message data = yield from self.http.send_message(channel_id, content, guild_id=guild_id, tts=tts, embed=embed) File "C:\Python36_64\lib\site-packages\discord\http.py", line 200, in request raise HTTPException(r, data) discord.errors.HTTPException: UNAUTHORIZED (status code: 401): 401: Unauthorized

1

1 Answers

0
votes

Your request is missing an Request Header. You must specify the User-Agent header that the Webserver allows you to connect.