Im making a cripto price tracker and i cant make it reply the json that im making with the coin decko URL and Arg1 for example Eternal or SLP, etc.
# bot = scrt.bot
bot = commands.Bot(command_prefix = "!")
login = 0
tokens_dict = {
'morw' : '0x6b61b24504a6378e1a99d2aa2a5efcb1f5627a3a',
'slp' : '0xcc8fa225d80b9c7d42f96e9570156c65d6caaa25',
'pvu' : '0x31471e0791fcdbe82fbf4c44943255e923f1b794',
'eternal' : '0xd44fd09d74cd13838f137b590497595d6b3feea4'
}
# Login
#bot.event
async def on_ready():
global login
print('We have logged in as {0.user}'.format(bot))
login = 1
#bot.command()
async def coin(ctx, arg1):
global tokens_dict
if(arg1 in tokens_dict.keys()):
url = 'https://api.pancakeswap.info/api/v2/tokens/' + tokens_dict[arg1]
response = request.get(url)
responseDict = json.loads(response.text)
await ctx.reply(responseDict)
else:
await ctx.reply("The token " + str(arg1) + " is not in the token list, if you want to add " + str(arg1) + " to the list please use the command : " + '\n' + "!add_token")
In the coin function im trying to reply the json that ive created but i dont know how to.
response = request.get(url)
responseDict = json.loads(response.text)
await ctx.reply(responseDict)
I'm assuming this is the part you're talking about.
You actually don't need to call json.loads(response.text) if you're going to send it anyway. If the raw response is unformatted then you can do this
response_dict = json.loads(response.text)
formatted_dict = json.dumps(response_dict, indent=4)
await ctx.reply(formatted_dict)
This should send the json as a string with indents.
The reason discord.py wasn't letting you send the message was because it wasn't a string.
Related
I want to make auto chat with 2 account, first I finished coding auto send message to specific channel, second i want to auto reply to first account.
here my code:
token = 'xxx.GImAw0.0G2MaMXjkvOLUAPfXYajK6eM-Lv5rFteEuutwY'
mess = open("messages.txt", "r").read().splitlines()
chd = 'xxx'
for line in mess:
url = 'https://discord.com/api/v9/channels/' + chd + '/messages'
payload = {"content": line, "tts": False}
header = mainHeader(token)
time.sleep(1)
src = requests.post(url, headers=header, json=payload)
if src.status_code == 429:
ratelimit = json.loads(src.content)
print(f"[!]Ratelimit for",
str(float(ratelimit['retry_after'])) + " seconds")
time.sleep(float(ratelimit['retry_after']))
I'm stuck on my coding to add auto reply with second account(token)
Just like the title, I'm wanting to use the pexels api in a slash command to get random images from pexels (or any other site) using nextcord bot, hosting on repl.it. Begging for guidance, thank you in advance. See code below
async def init(interaction: Interaction):
await interaction.response.defer()
async with aiohttp.ClientSession()as session:
async with session.get(url = "https://api.pexels.com/v1/curated", headers = {'Authorization': "23456789"}, data = {"per_page": "1"}) as response:
raw = await response.text()
raw = raw.replace('[', '').replace(']', '')
init = json.loads(raw)
url = init["url"]
myobj = {'per_page':'1'}
embed = nextcord.Embed(page = init["page"], timestamp = datetime.now(), color = nextcord.Colour.green())
try:
embed.add_field(name = "Copyright", value = init["copyright"])
except KeyError:
pass
embed.set_image(url = url, myobj = myobj)
embed.set_footer(text = f"From {init['date']}")
await interaction.followup.send(embed = embed)
I am creating a discord bot that gives me gif when I say !gif in a particular channel. The problem I am facing is when I type # it cannot change itself to %23 as it is used in links. I just want a way to change # in the string to %23. Please help me for that. The whole code is given below. I am very new to python so if you want any other errors please fix it and also clean up the code.I am using the tenor API Thank you
Code :
import discord
import os
import json
import requests
import random
client = discord.Client()
global search_term_public
global url
search_term_public = "Rick Roll"
def tenor():
global url
# set the apikey
apikey = (os.getenv("TENORAPIKEY"))
# our test search
search_term = search_term_public
# get the GIFs for the search term
r = requests.get("https://g.tenor.com/v1/search?q=%s&key=%s&contentfilter=high" % (search_term, apikey))
if r.status_code == 200:
# load the GIFs using the urls for the smaller GIF sizes
top_8gifs = json.loads(r.content)
g = len(top_8gifs['results'])
i = random.randint(0,g)
if(i == g):
i = g-1
h = str(g)
f = str(i)
url = top_8gifs['results'][i]['media'][0]['gif']['url']
print("The number picked is " + f +" out of " + h + ". Search Term : " + search_term + ". Url : " +url)
else:
top_8gifs = None
return url
#client.event
async def on_ready():
print("Bot has successfully logged in as {0.user}".format(client))
#client.event
async def on_message(message):
global search_term_public
if message.author == client:
return
if message.content.startswith("!gif") and message.channel.id == 831789159587774504:
# put the search term into the public variable. split the content with space and the second or more than second word should be in a variable
tokens = message.content.split(' ')
if tokens.__contains__(""):
tokens.remove("!gif")
tokens.remove("")
elif tokens.__contains__("#"):
# I want to change # in the token and change it to %23
print()
else :
tokens.remove("!gif")
search_term_public = ("".join(tokens))
if search_term_public == "":
search_term_public = "Rick Roll"
await message.channel.send("You got rick rolled!")
url = tenor()
await message.channel.send(url)
client.run(os.getenv("DISCORDTOKEN"))
Try this, it might work
You can just use the "replace" as follows
elif tokens.__contains__("#"):
token=token.replace("#","%23")
You want to look at
url encoding probably.
However, to directly answer the question as a hotfix, I think you can do this directly after the .split() line
tokens = [token.replace('#', '%23') for token in tokens]
I am wondering how do u make a loop for an api call that will keep calling that API, but when I tried making one it didn't work here is the code:
while True:
api_requesting = requests.get("https://api.battlemetrics.com/servers/3411152?include=player", headers=headers)
time.sleep(5)
jsoned_api = api_requesting.json()
function = jsoned_api["included"]
names = []
for person in function:
names.append(person['attributes']['name'])
And this is for e to call upon the request, and parsed it to give me the names of each player etc
#client.command(name='players')
async def createEmbed(ctx):
await ctx.send(f"{len(names)} players are online currently")
urString = ""
for name in names:
urString = urString + "> " + name + "\n"
urString = "```" + urString + "```"
await ctx.send(urString)
So I am wondering how will I make a loop for my request it's all the way at the beginning where it says while true: but when I run it the bot doesn't respond, and doesn't do anything.
If you want your code to stop when the bot does not respond:
success = True
while success:
api_requesting = requests.get("https://api.battlemetrics.com/servers/3411152?include=player", headers=headers)
# Success is True when the response status code is 200
success = api_requesting.status_code==200
But if you want to keep making requests, you can try:
while True:
api_requesting = requests.get("https://api.battlemetrics.com/servers/3411152?include=player", headers=headers)
if api_requesting.status_code == 200:
# Do something when the bot responds
else:
time.sleep(5)
# Do something else when the bot does not respond
StackOverflow.
I've been having a problem with a discord bot, here's the script:
def send():
url = "https://discordapp.com/api/webhooks/762125650546131005/lgYkjh-ILrag2sb3nzqUZfF1sg2mN2a0QeABaUq9dwl7qBTNL4EqWV00K62xWZ8_sNQ5"
data = {}
data["content"] = ""
data["username"] = "Suggestions"
data["embeds"] = []
embed = {}
embed["description"] = "**Author** » <#" + str(message.author.id) + ">\n **Suggestion** » " + str(args)
embed["title"] = "**New Suggestion!**"
data["embeds"].append(embed)
result = requests.post(url, data=json.dumps(data), headers={"Content-Type": "application/json"})
send()
await message.author.send("Thank you for your suggestion! We will look into it as soon as possible and will message you if it will be used.")
When I do ";suggestion fix bugs" it just sends to the webhook "fix" which is only the first word and I am struggling to fix this. Please may someone help?
Don't use requests with discord.py, it is sync and will block your bot, use a discord.Webhook with aiohttp to send a discord.Embed
Example:
from aiohttp import ClientSession
async with ClientSession() as session:
wh = discord.Webhook.from_url("<webhook url>", adapter=discord.AsyncWebhookAdapter(session))
e = discord.Embed()
e.title = "Hello I am an Embed"
...
await wh.send(embed=e)