Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Torii bot #120

Merged
merged 7 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/node/torii-bot/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DISCORD_TOKEN=<DISCORD TOKEN>
TORII=http://0.0.0.0:8080
DISCORD_CHANNEL_ID=<DISCORD CHANNEL>
177 changes: 177 additions & 0 deletions examples/node/torii-bot/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)

report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Runtime data

pids
_.pid
_.seed
\*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover

lib-cov

# Coverage directory used by tools like istanbul

coverage
\*.lcov

# nyc test coverage

.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)

.grunt

# Bower dependency directory (https://bower.io/)

bower_components

# node-waf configuration

.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)

build/Release

# Dependency directories

node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)

web_modules/

# TypeScript cache

\*.tsbuildinfo

# Optional npm cache directory

.npm

# Optional eslint cache

.eslintcache

# Optional stylelint cache

.stylelintcache

# Microbundle cache

.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history

.node_repl_history

# Output of 'npm pack'

\*.tgz

# Yarn Integrity file

.yarn-integrity

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)

.cache
.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

.cache/

# Comment in the public line in if your project uses Gatsby and not Next.js

# https://nextjs.org/blog/next-9-1#public-directory-support

# public

# vuepress build output

.vuepress/dist

# vuepress v2.x temp and cache directory

.temp
.cache

# Docusaurus cache and generated files

.docusaurus

# Serverless directories

.serverless/

# FuseBox cache

.fusebox/

# DynamoDB Local files

.dynamodb/

# TernJS port file

.tern-port

# Stores VSCode versions used for testing VSCode extensions

.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.\*

# IntelliJ based IDEs
.idea


.env

dist
101 changes: 101 additions & 0 deletions examples/node/torii-bot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
## Torii Bot

A Discord bot for interacting with your Dojo world via Torii.

Features:

- Poll a Torii for information.
- Expose a slash command for reading Torii state.
- Easy to extend with new commands.
- GraphQL codegen for Torii models.

---

### Setup

> Follow the steps [below](#create-discord-bot-step-by-step) to obtain your Discord Token. You will also need to acquire your Discord channel ID where you want to send messages. You can obtain this by right-clicking on a channel in your Discord and selecting 'Copy Channel ID'.

### Developing

```bash
bun install
```

### Terminal 1 - Serve the Bot

```bash
bun run serve
```

### Terminal 2 - Build and Watch

```bash
bun run build --watch
```

Now, try running it on your server. Remember to restart your bot after making changes.

### Adding Torii GraphQL Models

Torii exposes dynamically generated models based on your world's models. We can use this to generate a GraphQL SDK for easy querying of the world.

You can access the GraphQL dashboard by navigating to [http://0.0.0.0:8080/graphql](http://0.0.0.0:8080/graphql) if your Torii is running locally.

#### Codegen

Add your GraphQL schema to `src/graphql/schema.graphql`, then run

```bash
bun run codegen
```

Now you can access the sdk in your app like:

```js
import { sdk } from "../config.js";

const { data } = await sdk.getMoves({ player });
```

### Create discord bot step by step

### Create a Discord Bot Step by Step

> Step 1: Go to [Discord Developers](https://discord.com/developers/applications) and create a new application.
> ![Step 1](./images/Step%201.png)

> Step 2: Name the application.
> ![Step 2](./images/Step%202.png)

> Step 3: Select the 'Bot' sidebar item and reset and save the Auth token. Store it in a .env file within your application. Do not share or commit this token. Ensure its confidentiality.
> ![Step 3](./images/Step%203.png)

> Step 4: Enable the switches as shown in the image below. Your app will not function properly without doing this.
> ![Step 4](./images/Step%204.png)

> Step 5: Choose the 'OAuth2' sidebar item and make the selections as shown. Opt for additional choices if you understand their implications.
> ![Step 5](./images/Step%205.png)

> Step 6: Determine the permissions for the bot. As our goal is only to send messages, that's what we'll select.
> ![Step 6](./images/Step%206.png)

> Step 7: Navigate to the URL generated in the previous step and add the bot to your server.
> ![Step 7](./images/Step%207.png)

### Deploying

Deploying these apps onto [Railway](https://railway.app/) is the easiest.

1. Create Account
2. Create a new project and deploy via git
3. In your project settings

Update the project settings:

**Build Command:**
`bun run build`

**Run Commands:**
`bun run serve`

OHAYO
Binary file added examples/node/torii-bot/bun.lockb
Binary file not shown.
24 changes: 24 additions & 0 deletions examples/node/torii-bot/codegen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { CodegenConfig } from "@graphql-codegen/cli";

const config: CodegenConfig = {
schema: "http://0.0.0.0:8080" + "/graphql",
documents: "src/**/*.graphql",
generates: {
"src/generated/graphql.ts": {
plugins: [
"typescript",
"typescript-operations",
"typescript-graphql-request",
],
config: {
rawRequest: true,
},
},
},
hooks: {
afterAllFileWrite: [
`sed -i '' "s/import { GraphQLClientRequestHeaders } from 'graphql-request\\/build\\/cjs\\/types';/export type GraphQLClientRequestHeaders = Headers | string\\[\\]\\[\\] | Record<string, string>/" ./src/generated/graphql.ts`,
],
},
};
export default config;
28 changes: 28 additions & 0 deletions examples/node/torii-bot/dojoConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import manifest from "../../dojo-starter/target/dev/manifest.json" assert { type: "json" };

const {
NODE_URL,
TORII,
MASTER_ADDRESS,
MASTER_PRIVATE_KEY,
ACCOUNT_CLASS_HASH,
} = process.env;

export type Config = ReturnType<typeof dojoConfig>;

export function dojoConfig() {
return {
rpcUrl: NODE_URL || "http://localhost:5050",
toriiUrl: TORII || "http://0.0.0.0:8080",
masterAddress:
MASTER_ADDRESS ||
"0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03",
masterPrivateKey:
MASTER_PRIVATE_KEY ||
"0x1800000000300000180000000000030000000000003006001800006600",
accountClassHash:
ACCOUNT_CLASS_HASH ||
"0x05400e90f7e0ae78bd02c77cd75527280470e2fe19c54970dd79dc37a9d3645c",
manifest,
};
}
Binary file added examples/node/torii-bot/images/Step 1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/node/torii-bot/images/Step 2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/node/torii-bot/images/Step 3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/node/torii-bot/images/Step 4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/node/torii-bot/images/Step 5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/node/torii-bot/images/Step 6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/node/torii-bot/images/Step 7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/node/torii-bot/images/railway.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions examples/node/torii-bot/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "torii-bo",
"module": "index.ts",
"type": "module",
"main": "dist/src/server.js",
"devDependencies": {
"@graphql-codegen/cli": "^5.0.0",
"@graphql-codegen/typescript": "^4.0.1",
"graphql": "^16.8.1",
"@graphql-codegen/typescript-graphql-request": "^6.0.0",
"graphql-request": "^6.1.0",
"@graphql-codegen/typescript-operations": "^4.0.1",
"@types/express": "^4.17.17",
"@types/node": "^20.11.10",
"@types/node-cron": "^3.0.10",
"bun-types": "latest"
},
"dependencies": {
"@pinecone-database/pinecone": "^1.1.2",
"@sapphire/decorators": "^6.0.2",
"@sapphire/framework": "^4.6.0",
"discord.js": "^14.13.0",
"express": "^4.18.2",
"langchain": "^0.0.200",
"node-cron": "^3.0.2",
"openai": "^4.20.1",
"twitter-api-v2": "^1.15.2",
"typescript": "^5.2.2"
},
"scripts": {
"build": "tsc",
"serve": "tsc && node dist/node/torii-bot/src/index.js",
"codegen": "graphql-codegen"
}
}
Loading
Loading